Updating the Controller

Go back to our Controller class.

  1. Call the ComicBookRepository class.
    private ComicBookRepository _comicBookRepository = null;
    
  2. Create a constructor for ComicBooksController class.
    public ComicBooksController()
    {
        _comicBookRepository = new ComicBookRepository();
    }
    
  3. Change our ActionResult to be like this.

    public ActionResult Detail(int id)
    {
        var comicBook = _comicBookRepository.GetComicBook(id);
    
        return View(comicBook);
    }
    
  4. Take a look at our controller, it doesn't show anything because we already replaced the view.

  5. Got an error, huh?
  6. URL Routing pattern that we know -> controller/action. The actual pattern is controller/action/id.
  7. 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)
    
  8. 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);
    }
    

results matching ""

    No results matching ""