I have a view with a table that displays my model items. I\'ve extracted the relevant portions of my view:
@model System.Collections.Generic.IEnumerable
You cannot use a foreach
loop to generate form controls for properties in a collection. It creates duplicate name
attributes (in your case name="item.IncludeProvision"
) which have no relationship to your model and duplicate id
attributes which is invalid html. Use either a for
loop (you models needs to be IList
for(int i = 0; i < Model.Count; i++)
{
....
@Html.CheckBoxFor(m => m[i].IncludeProvision)
}
or create an EditorTemplate
for typeof Provision
. In /Views/Shared/EditorTemplates/Provision.cshtml
(note the name of the template must match the name of the type)
@model Provision
....
@Html.CheckBoxFor(m => m.IncludeProvision)
and in the main view (the model can be IEnumerable
)
@Html.EditorFor(m => m)