I have a follow up question on the accepted answer given here: Two models in one view in ASP MVC 3
I have three models, Types, Causes, Locations that I would like to list the contents of in one view. Based on the answer in the link above I made a new model that combined the that looks like this:
public class Combined
{
public IEnumerable<Place> Place { get; set; }
public IEnumerable<Type> Type { get; set; }
public IEnumerable<Cause> Cause { get; set; }
}
I made it IEnumerable<> because as I understand it, thats what I want when I just want to list the contents of these models in a foreach loop. Then I made this controller for the view:
[ChildActionOnly]
public ActionResult overSightHeadings()
{
Combined Combined = new Combined();
return View(Combined);
}
And finally the view (I was just trying to list from one of the tables first):
@model mvcAvvikelser.Models.Combined
@{
Layout = null;
}
<tr>
@foreach (var Type in Model.Type)
{
<th> @Html.DisplayTextFor(ModelItem => Type.Name)</th>
}
</tr>
The problem with this code is that it throws up a null exception when the foreach code starts.
System.NullReferenceException: Object reference not set to an instance of an object.
So I am not entirely sure what I am doing wrong here, should it not be an IEnumerable, have I initialized the model incorrectly in the controller?