ASP.NET MVC Complex Object property stays null on form submit
Solution 1:
Model binding to a list doesn't work with a foreach
; you need to use a for
loop instead.
You'll also need hidden inputs for any properties which don't have editors within the loop.
@for (int index = 0; index < Model.Items.Count; index++)
{
<tr>
<td>
@Html.HiddenFor(m => m.Items[index].Id)
@Model.Items[index].Id
</td>
<td>
@Html.EditorFor(m => m.Items[index].Task)
</td>
<td>
@Html.EditorFor(m => m.Items[index].IsDone, new { htmlAttributes = new { @Style = "margin-left: 10px;" } })
</td>
</tr>
}
ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries - Scott Hanselman's Blog