I have a view that is populated from a view model with one of the properties being a collection of complex types. The view renders correctly with the list being iterated into a table with a radio button for each item which has not already been selected. When I submit the form I cannot get the list of objects back into the model and I can't figure out what I'm doing wrong. Each item in the collection is added to the view as below:
@for (int i = 0; i < Model.Items.Count; i++
var rowClass = "selectRow";
if (item.IsSelected)
{
rowClass = "success";
}
<tr class="@rowClass">
@Html.HiddenFor(m => m.Items[i].PropertyOne)
<td>
@Html.DisplayFor(m => m.Items[i].PropertyTwo)
</td>
<td class="actions">
// only those items not previously selected need a radio button
@if (item.IsSelected == false)
{
@Html.RadioButtonFor(m => m.Items[i].IsSelected, m.Items[i].PropertyOne, new { id = "IsSelected_" + m.Items[i].PropertyOne })
}
</td>
</tr>
}
I have tried using @Html.HiddenFor(x => x.PropertyOne) but I cannot bind the collection of selected values back to the model. I can return everything else in the model using hidden fields but I have no clue how to fix this. Any help is appreciated.
Thanks