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.

有帮助吗?

解决方案

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/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top