MVC3 how to post a list within a class in controller?

后端 未结 3 2072
傲寒
傲寒 2021-01-27 03:42

I have a class:

public class CarList
{
    public int quantity{get;set;}
    public List Cars {get;set;}
}

public class Car {
    public string Name          


        
3条回答
  •  遇见更好的自我
    2021-01-27 04:17

    I think this is the issue:

    @foreach (Car item in Model.Cars)
           {
               @Html.EditorFor(x=>item);
           }
    

    Change it to

    @foreach (Car item in Model.Cars)
           {
               @Html.EditorFor(x=>item.Name);
           }
    

    It might a case of the model binder not being smart enough to bind more than one level down, although I don't remember ever having that issue. it may also help to add Glimpse (http://getglimpse.com/) to your project so that you can see how the request is actually processed.

提交回复
热议问题