Updating the Controller
Go back to our Controller class.
- Call the ComicBookRepository class.
private ComicBookRepository _comicBookRepository = null;
- Create a constructor for ComicBooksController class.
public ComicBooksController() { _comicBookRepository = new ComicBookRepository(); }
Change our ActionResult to be like this.
public ActionResult Detail(int id) { var comicBook = _comicBookRepository.GetComicBook(id); return View(comicBook); }
Take a look at our controller, it doesn't show anything because we already replaced the view.
- Got an error, huh?
- URL Routing pattern that we know -> controller/action. The actual pattern is controller/action/id.
- Actually, MVC stills recognize both of the pattern, but it confusing. Unfortunately, our Detail method needs an input that not nullable. To make the method receive a nullable input, try change to this.
public ActionResult Detail(int? id)
Now, let's code some conditional operation if the id is null.
public ActionResult Detail(int? id) { if (id == null) return HttpNotFound(); var comicBook = _comicBookRepository.GetComicBook((int) id); return View(comicBook); }