ModelBinding on model collection

Lasse Vabe Rolstad

I am trying to create a very simple form to post back some values from a collection of models

When i click submit, and look at the collection returned it does not contain any objects. I also se that the asp-for does not generate the collection index that i expected.

I have a simple model

public class CustomModel
{
    public int Id { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
}

And this is my view

@model ICollection<CustomModel>
<form asp-action="Index" asp-controller="Home" method="POST">
<table>
    @foreach (var m in Model)
    {
        <tr>
            <td><label asp-for="@Model">@m.Question</label><input asp-for="@m.Answer"/></td>
        </tr>
    }
</table>
<input type="submit" value="save" />

Example of how this would look when the page is renderd:

<tr>
            <td><label>Your name?</label><input type="text" id="m_Answer" name="m.Answer" value="" /></td>
        </tr>
        <tr>
            <td><label>Your Age?</label><input type="text" id="m_Answer" name="m.Answer" value="" /></td>
        </tr>

This is were i assumed it would have a index, but instead it looks like it treats each row as an induvidal model instead of a collection of models.

What am i doing wrong here? Is this a bug, or by design?

Github test project https://github.com/lasrol/TestModelBindingList

YuriyP

Change your model to @model List<CustomModel> And than use next approach

<form asp-action="Index" asp-controller="Home" method="POST">
<table>
    @for (int i = 0; i < Model.Count; i++)
    {            
        <tr>
            <td>
                <input type="hidden" asp-for="@Model[i].Id" />
                <input type="hidden" asp-for="@Model[i].Question" />            
                <label asp-for="@Model[i].Question">@(Model[i].Question)</label>
                <input asp-for="@Model[i].Answer" />
            </td>
        </tr>            
    }
</table>
<input type="submit" value="save" />

So as you can see you should access to list items via index to properly render name attribute for inputs. Also do not forget to include other item properties via hidden inputs otherwise it values will be loosed in post action.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

modelBinding works when using model but not with viewmodel

From Dev

Model collection inside a Model

From Dev

Model collection inside a Model

From Dev

how to pass the result model object out of System.Web.Http.ModelBinding.IModelBinder. BindModel?

From Dev

Cannot bind model to collection

From Dev

Model Binding to an object with a collection

From Dev

MVC Nested Model Collection

From Dev

Adding model to a collection not working

From Dev

Collection inside a Backbone model

From Dev

Collection-less model

From Dev

MVC Nested Model Collection

From Dev

ModelBinding parameter list in MVC

From Dev

ModelBinding not deserializing json

From Dev

ModelBinding parameter list in MVC

From Dev

ModelBinding to DataTables Order data

From Dev

Map Async Model Collection to Async ViewModel Collection

From Dev

Map Async Model Collection to Async ViewModel Collection

From Dev

Bind a collection of a model to only a part of another collection

From Dev

Unable to model bind a collection within a view model

From Dev

Model not being pushed into collection of backbonejs

From Dev

Remove collection from model RacerJS?

From Dev

Backbone populate Model inside of a Collection

From Dev

Laravel Pagination with hydrated Model collection

From Dev

how to update model is part of a collection

From Dev

get model triggering events in collection

From Dev

JSONModel library / model collection wrong

From Dev

Model not being pushed into collection of backbonejs

From Dev

Backbone Collection not using it's model

From Dev

Collection - Model usage for JSON response?