ASP.NET MVC DefaultModelBinder with nested lists

最后都变了- 提交于 2019-12-04 00:22:24

I eventually figured out why DefaultModelBinder wasn't picking up on the properties of WorkUnit: Because they weren't properties, they were fields. DefaultModelBinder only works with properties. Changing the class definition of WorkUnit and Project to use fields made everything click:

public class Project {
    public IList<WorkUnit> WorkUnits { get; set; }
    public string Name { get; set; }
}

public class WorkUnit {
    public DateTime Date { get; set; }
    public decimal RegularHours { get; set; }
    public decimal OvertimeHours { get; set; }
}

(Note: The source code in the original question had Project.Name defined as a field, in my actual code it was a property. This is why the Projects list was getting populated but WorkUnits wasn't.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!