Bare in mind, I'm quite new to ASP.NET MVC. So using EF code first method, I created 2 models, Movie and Producer:
public class Movie {
public int ID {get;set;}
public string Name {get;set;}
public string Genre {get;set;}
public Producer Producer {get;set;}
}
public class Producer {
public int ID {get;set;}
public string Name {get;set;}
public DateTime DOB {get;set;}
public List<Movie> Movies {get;set;}
}
And in the controller class "Movies", I called a view:
public class MoviesController : Controller
{
//context just has DbSet< .. > of both classes.
MoviesContext db = new MoviesContext();
public ActionResult Index()
{
var movies = from m in db.Movies
select m;
return View(movies.ToList());
}
}
But if I call the producer within the view
@foreach(var item in Model)
{
<p>@item.Producer.Name</p>
}
MVC crashes with "Object reference not set to an instance of an object." error, even though when I look at the database, the Producer_ID field (which Code first made) was filled in and all of the producers with the according ID's exist in the database as well.
I did put the values inside the database manually, is that what would be causing it?
Any kind of help is more than appreciated! :)