(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.