带有嵌套列表的 ASP.NET MVC DefaultModelBinder
我有一个视图,其中有一个代表员工时间表的表。 顶部的天数,向下的项目,每个日期/项目交叉点都包含正常工作时间和加班时间的两个值。
页面模型的(简化的)类定义为:
public class TimesheetFormModel {
public List<Project> Projects;
// other things...
}
public class Project {
public string Name;
public List<WorkUnit> WorkUnits;
}
public class WorkUnit {
public DateTime Date;
public decimal RegularHours;
public decimal OvertimeHours;
}
页面上的表单元素命名如下,试图让 DefaultModelBinder 识别它们。
model.Projects[0].Name // "New Project"
model.Projects[0].WorkUnits[0].Date // "5/23/2009 12:00:00 AM"
model.Projects[0].WorkUnits[0].RegularHours // 0
model.Projects[0].WorkUnits[0].OvertimeHours // 0
model.Projects[0].WorkUnits[1].Date // "5/24/2009 12:00:00 AM"
model.Projects[0].WorkUnits[1].RegularHours // 0
model.Projects[0].WorkUnits[1].OvertimeHours // 0
model.Projects[0].WorkUnits[2].Date // "5/25/2009 12:00:00 AM"
model.Projects[0].WorkUnits[2].RegularHours // 0
model.Projects[0].WorkUnits[2].OvertimeHours // 0
// etc.
然而,当提交视图时,model
参数并未完全填充。 model.Projects
包含项目,但 Project
的 WorkUnits
字段为空。 DefaultModelBinder
是否支持嵌套集合,就像我想做的那样? 如果没有,我该怎么办?
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 regular hours and overtime.
The (simplified) class definitions for the page model are:
public class TimesheetFormModel {
public List<Project> Projects;
// other things...
}
public class Project {
public string Name;
public List<WorkUnit> WorkUnits;
}
public class WorkUnit {
public DateTime Date;
public decimal RegularHours;
public decimal OvertimeHours;
}
The form elements on the page are named as follows in an attempt to get the DefaultModelBinder to pick up on them.
model.Projects[0].Name // "New Project"
model.Projects[0].WorkUnits[0].Date // "5/23/2009 12:00:00 AM"
model.Projects[0].WorkUnits[0].RegularHours // 0
model.Projects[0].WorkUnits[0].OvertimeHours // 0
model.Projects[0].WorkUnits[1].Date // "5/24/2009 12:00:00 AM"
model.Projects[0].WorkUnits[1].RegularHours // 0
model.Projects[0].WorkUnits[1].OvertimeHours // 0
model.Projects[0].WorkUnits[2].Date // "5/25/2009 12:00:00 AM"
model.Projects[0].WorkUnits[2].RegularHours // 0
model.Projects[0].WorkUnits[2].OvertimeHours // 0
// etc.
When the view is submitted however, the model
parameter isn't being completely populated. model.Projects
contains projects, but the Project
's WorkUnits
field is empty. Does the DefaultModelBinder
support nested collections like I'm trying to do? If not, what should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终明白了为什么
DefaultModelBinder
没有获取WorkUnit
的属性:因为它们不是属性,而是字段。DefaultModelBinder
仅适用于属性。 更改WorkUnit
和Project
的类定义以使用字段使所有内容都单击:(注意:原始问题中的源代码将 Project.Name 定义为字段,在我的实际代码它是一个属性。这就是为什么项目列表被填充但工作单元却没有。)
I eventually figured out why
DefaultModelBinder
wasn't picking up on the properties ofWorkUnit
: Because they weren't properties, they were fields.DefaultModelBinder
only works with properties. Changing the class definition ofWorkUnit
andProject
to use fields made everything click:(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.)