Question

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.

Was it helpful?

Solution

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top