Multiple-Add in a single View, ASP.NET MVC

1.6k views Asked by At

I'm trying to add multiple items of the same type at once in a single View, while giving the model.

@model List<Item>

For the view, It renders, when I post back, the model is null, even though the form data is sent correctly but for some reason the mapping is not happening.

1

There are 1 answers

3
hutchonoid On BEST ANSWER

For complex items you need to index your collections for the model binder.

Change your loop to this which will be picked up by the model binder (without seeing your view or model I am using Field for demo purposes here).

   @for (int i = 0; i < Model.Count; i++)
    {
        .....
        @Html.EditorFor(model => Model[i].Field)
        .....
    }

They will then be posted back to the server.

For more info on it see here:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/