My main view model has a ChildViewModel collection. In the view, I iterate over the collection and call EditorFor (), for example:
@for (int i = 0; i < Model.Children.Count; i++) { @Html.EditorFor(m => m.Child[i]); }
The editor template looks like this:
@model ChildModel @using (Html.BeginForm("EditChild", "MyController")) { @Html.HiddenFor(m => m.ChildId) @Html.TextBoxFor(m => m.ChildName) }
This will create markup where each child will be in a separate form, and each such form will have an input control named Child [0] .ChildName. I use a separate form for each child, as the children will be displayed one on each line, and then the user can edit and send one line.
My form action method:
[HttpPost] public ActionResult EditChild(ChildViewModel form) { }
The problem is that when this is called, all the properties of the model will be empty, because the connecting device does not know about the prefix. In some situations, we can use BindAttribute to inform the model binding of the prefix, but in this case the prefix is not constant: it will be Child [0], Child [1], etc.
So, we want to repeat the same form for each row in the collection, and then allow the POST user one form. How can a website handle the identifier, name, prefix, and model binding in this scenario?