I have a model with a property of type List. I use this property to show values on my view. I use a foreach loop to go through each item in the list and displaying them using DisplayFor and TextBoxFor. That part is working fine; I've used TextBoxFor before and any user entered text gets past to the model on submit. However, when using a list, the list is null when the form is submitted. All the other properties in the model are updated and I can access them correctly. What is preventing the list from binding and how can I fix this?
ModelInstrumentListingDetail
public class ModelInstrumentListingDetail
{
public int InstrumentTagID { get; set; }
public string InstrumentTagDescription { get; set; }
public List<ModelInstrumentListingItem> items { get; set; }
}
ModelInstrumentListingItem
public class ModelInstrumentListingItem
{
public string Field { get; set; }
public string Label { get; set; }
}
View
@model GPC.Models.ModelInstrumentListingDetail
@using (Html.BeginForm("InstrumentListingAdd", "Home", FormMethod.Post, new { id = "InstrumentListingDetailForm" }))
{
<table>
<tr>
<td>
@Html.TextBoxFor(m => m.InstrumentTagDescription)
</td>
</tr>
@foreach (GPC.Models.ModelInstrumentListingItem item in Model.items)
{
<tr>
<td>
@Html.DisplayFor(m => item.Label)
</td>
<td>
@Html.TextBoxFor(m => item.Field)
</td>
</tr>
}
</table>
<input id="submitInstrumentListingAdd" name="submitInstrumentListingAdd" value="Add" type="submit" class="SaveButton" />
}
Controller
[HttpPost]
public ActionResult InstrumentListingAdd(ModelInstrumentListingDetail model, string submitInstrumentListingAdd)
{
...
}
On submit, the model posted to the controller has InstrumentDescription with a value, but items is null. I need to figure out how to fill items with the new values.