—EDIT—
I have this model
public class MainModel {
string Name;
List<Address> Addresses;
}
public class Address {
string prop2;
}
Is quite easy to create the view but I didn’t find a way to bind it in the POST request. I would like to avoid to loop on Request.Form
View
@foreach (var obj in Model.Address)
{
<tr>
<td>
<input type="text" id="@string.Format("obj.Name.{0}", obj.Id)" name="@string.Format("obj.Name.{0}", obj.Id)" value="@Model.Name" />
//I'm not able to overwrite the name attribute, still working on that
</td>
</tr>
}
Is there a way to automatically bind this list in the ActionController? Something like
public ActionResult Edit(MainModel model, List<Address> objects)
I suppose I have to create a custom model binding but I don’t find the way to do it
… MVC make us lazy … 🙂
From reading you’re requirements you may be over thinking the solution.
In the Views/EditorTemplates folder create a view Address.cshtml similar to
In your Edit view have markup similar to:
The EditorFor statement will use the Address editor template to render the mark up.
If you need the ability to add new addresses you can use client side scripting to find the highest id and inject the following mark up
Then you controller action becomes