This has been a thorn in my side for a while. If I use EditorFor on an array of objects and the editor Template has a form in it ex.
public class FooController:Controller {
public ActionResult Action(Foo foo) {
// ...
}
}
Index.cshtml
@model IEnumerable<Foo>
@Html.EditorFor(m=> m)
EditorTemplate
@model Foo
@using (Html.BeginForm("action", "controller"))
{
@Html.TextBoxFor(f=> f.A)
@Html.CheckBoxFor(f=> f.B)
@Html.LabelFor(f=> f.B)
}
So I'll hit a few problems.
The checkbox label's for doesn't bind correctly to the checkbox (This has to do with the label not receiving the proper name of the property ([0].A
as opposed to A
).
I'm aware I can get rid of the pre- text by doing a foreach on the model in Index but that screws up ids and naming as the framework doesnt realize there are multiples of the same item and give them the same names.
For the checkboxes I've just been doing it manually as such.
@Html.CheckBoxFor(m => m.A, new {id= Html.NameFor(m => m.A)})
<label for="@Html.NameFor(m => m.A)">A</label>
However I cant solve the inability of the controller to accept the item as a single model. I've even tried allowing an array of Foo's in the Action
parameters but that only work when its the first item being edited ([0]...
) if its any other item in the array (ex. [1].A
) the controller doesn't know how to parse it. Any help would be appreciated.