Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

kevin hudson
PLUS
kevin hudson
Courses Plus Student 11,987 Points

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
Allan Clark
10,810 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.

kevin hudson
PLUS
kevin hudson
Courses Plus Student 11,987 Points

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);


        }