0

(Still Unsolved)

So, I could use some help from one of you guys in here. I'm currently working on a comic book store website, where I'm trying to grap infomation from my model. I think it's easier to just show code and pictures so you can see exactly what's going on:

        public ActionResult Comic(int id)
    {
        ComicVM vm = new ComicVM();
        vm.Comic = db.Comics.Find(id);
        vm.Series = db.Series.FirstOrDefault();

        return View(vm);
    }

As you can see I'm trying to find a specific comicbook by id in the controller and then have that specific comic book also say what comic book series it's from.

    public class Comic
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int PageAmount { get; set; }
    public DateTime DateReleased { get; set; }
    public DateTime Uploaded { get; set; }
    public string ImageUrl { get; set; }

    public Publisher Publisher { get; set; }

    public int SeriesId { get; set; }
    public Series Series { get; set; }
}

This is the model I use.

    public class ComicVM
{
    public Comic Comic { get; set; }
    public Series Series { get; set; }
    public Publisher Publisher { get; set; }
}

My viewmodel, which is used by the "vm" in the controller

        @model ComicbookWebpage.Models.ViewModels.ComicVM
@{
    ViewBag.Title = "Comic";
}

<h5><b>Title:</b> @Model.Comic.Title</h5>
<h5><b>Series:</b> @Model.Comic.Series.Title</h5>
<h5><b>Pages:</b> @Model.Comic.PageAmount</h5>

And last, the view that I render my html in. As you can see on the Series line, I try to get this comic book's specific Series it's in but everytime I try to it gives me a "Object reference not set to an instance of an object". Is there something I have to do in the controller to let it know a specific series to pull from every comicbook? I only have 2 series at the moment in my Series table and one of them have 2 comicbooks and the last one has 1.

Hope it all makes sense and thanks in advance.

  • Your `public Series Series` and `public Publisher Publisher` properties make no sense - they are already properties in your model. And in anycase you downloading the 1st` Series` in the database which is unlikely to be the one associated with the `Comic` your downloading. Use `vm.Comic = db.Comics.Where(x => x.Id == id);`, but depending on your setup you may need to use `db.Comics.Include(x => x.Series).Where(x => x.Id == id);` –  Feb 14 '18 at 22:47
  • And you may as well not have a view model if this is just for display. but its its for editing data, then use a view models but view models do not contain data models - refer [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Feb 14 '18 at 22:49
  • The "Include" won't let me add Id like that. The intellisense won't catch the code sorter speak. Also I see your point on the viewmodel, I'll get it fixed. – René Pedersen Feb 14 '18 at 23:03
  • Then your probably not include `using Microsoft.Data.Entity;` –  Feb 14 '18 at 23:05
  • Note also in the code above, it should have been `.Where(x => x.Id == id).FirstOrDefault();` –  Feb 14 '18 at 23:10
  • It doesn't work mate, I've tried many combinations with the code you provided above... – René Pedersen Feb 14 '18 at 23:19
  • Then you have not set up your navigation properties. I suggest a bit of research, starting with [Entity Framework Relationships and Navigation Properties](https://msdn.microsoft.com/en-us/library/jj713564(v=vs.113).aspx) –  Feb 14 '18 at 23:21
  • @RenéPedersen, if you are getting **Object reference not set to an instance of an object** error in view that means you Comic object (not entire model) which you are returning from controller is null that means you are getting null value when you do Find(id)..please check once – Ray Feb 15 '18 at 05:52
  • I believe this should be vm.Comic.Series = db.Series.FirstOrDefault(); and not vm.Series = db.Series.FirstOrDefault(); since you access it like Model.Comic.Series.Title and not Model.Series – Ivan Zaruba Feb 15 '18 at 08:46

3 Answers3

0

you are not looking for the series of that specific comic, you are looking to all series, try:

public ActionResult Comic(int id)
{
    ComicVM vm = new ComicVM();
    vm.Comic = db.Comics.Find(id);
    vm.Series = db.Series.FirstOrDefault();  //this is looking to all series
    vm.Series = vm.Comic.Series;

    return View(vm);
}
Josue Martinez
  • 436
  • 5
  • 14
0

Try this

public ActionResult Comic(int id)
{
    ComicVM vm = new ComicVM();
    vm.Comic = db.Comics.Find(id);
    vm.Comic.Series = db.Series.FirstOrDefault(); // <--- (!)

    return View(vm);
}
Ivan Zaruba
  • 4,236
  • 5
  • 20
  • 29
  • Sorry for the late response, I just got home. So I tested this and I was pretty sure I already tried that code earlier, but it just ends up giving me one series for all 3 different comic books, so they all take series id 1, also if I have a comic with series id 2. – René Pedersen Feb 15 '18 at 18:10
  • Well, with the code you provided you had exactly the same logic and the problem was a null reference exception. I gave you a solution to make your code run, I didn't care the business logic. You'd better explained your requirements then. – Ivan Zaruba Feb 15 '18 at 22:47
  • I stated clearly in my post that I wanted my code to grap a comic book and then have the series it's in, listed in the same view as the book. But I appreciate you trying to help and I'm sorry for not making myself clear enough to you. But as you can see, I posted the solution, in case somebody else runs into the same problem. Thanks for your time though. – René Pedersen Feb 15 '18 at 22:56
0

I found a solution with the code Stephen Muecke provided, it was just a matter of writing the code and relations correctly, as he also stated in his comment.

Here is the solution:

        public ActionResult Comic(int id)
    {
        ComicVM vm = new ComicVM();
        vm.Comic = db.Comics.Include(m => m.Series).Where(m => m.Id == id).FirstOrDefault();

        return View(vm);
    }