ASP.NET MVC DefaultModelBinder with nested lists

前端 未结 1 532
旧时难觅i
旧时难觅i 2021-02-19 11:49

I have a View with a table representing an employee\'s timesheet. Days across the top, projects down the side, with each day/project intersection containing two values for regul

1条回答
  •  旧时难觅i
    2021-02-19 12:30

    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 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.)

    0 讨论(0)
提交回复
热议问题