C# ASP.NET MVC Basics Modeling and Presenting Data Adding the Repository

Error with ComicBooksController

SO the instructor didn't get an error at the end when he built the project but he should have because in the controller class he didn't update the Detail() return View

This was left:

 public ActionResult Detail()
        {


            // return model instance comicbook 
            return View(comicBook);


        }

What should go here?

2 Answers

Allan Clark
MOD
Allan Clark
Treehouse Moderator 10,799 Points

The video linked to this question does not seem to match up to your question as it is all about the repository class.

However to address your question, generally a Detail controller action should take in an int as a parameter. This int will be used to look up the entity that will be displayed. It should look something like the following:

public ActionResult Detail(int id)
        {
            var comicBook = ComicBookRepository.GetById(id);
            return View(comicBook);
        }

Please note this is essentially pseudo code and may not directly match up to how the project is structured, but it should give you an idea of what should go on in this controller action.

Wasn't exactly the answer but pretty close. It was addressed in the next video, I think. But what had happened was the instructor had kept all the comicbook array object data in the controller at that time even though he had moved it from the controller to a new class repo so it was still working correctly. I had cut and pasted the data out.

 public ActionResult Detail(int? id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }
            var comicBook = _comicBookRepository.GetComicBook(id.Value);
            // return model instance comicbook 
            return View(comicBook);


        }