带有嵌套列表的 ASP.NET MVC DefaultModelBinder

发布于 2024-07-24 07:24:34 字数 1254 浏览 5 评论 0原文

我有一个视图,其中有一个代表员工时间表的表。 顶部的天数,向下的项目,每个日期/项目交叉点都包含正常工作时间和加班时间的两个值。

页面模型的(简化的)类定义为:

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 包含项目,但 ProjectWorkUnits 字段为空。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2024-07-31 07:24:35

我最终明白了为什么 DefaultModelBinder 没有获取 WorkUnit 的属性:因为它们不是属性,而是字段DefaultModelBinder 仅适用于属性。 更改 WorkUnitProject 的类定义以使用字段使所有内容都单击:(

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; }
}

注意:原始问题中的源代码将 Project.Name 定义为字段,在我的实际代码它是一个属性。这就是为什么项目列表被填充但工作单元却没有。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文