[英]MVC C# Automatic Model Binding in Nested Lists
This is my first ever C# / MVC project and I am having problems binding to a model. 这是我的第一个C#/ MVC项目,我遇到了与模型绑定的问题。 I have read an applied Phil Haack's Post and have used EditorFor for all my partial views. 我已经阅读了应用Phil Haack的帖子 ,并使用了EditorFor来获取所有部分视图。 Do I need a custom Model Binder? 我需要自定义型号粘合剂吗? Please help 请帮忙
In brief, I have a list of weeks which contain a list of entries. 简而言之,我有一个包含条目列表的周列表。 These entries contain a list of hours 这些条目包含小时列表
Action: 行动:
[HttpPost]
public ActionResult SubmitRecords(List<WeekCollection> itemData)
{
//do stuff
return View();
}
Model: 模型:
public class WeekCollection
{
public WeekCollection()
{
this.OneWeek = new List<Entry>();
}
public List<Entry> OneWeek { get; set; }
}
[Bind(Exclude = "Task, Project")]
public class Entry
{
public int ProjectId { get; set; }
public virtual Projects Project { get; set; }
public int TaskId { get; set; }
public virtual Tasks Task { get; set; }
public bool Billable { get; set; }
public List<Hours> Gethours { get; set; }
}
public class Hours
{
public float NumberOfHours { get; set; }
}
Views (Index) 意见(指数)
//within partial view iteration with incrementing u (u++)
@using(@Html.BeginForm())
{
@Html.EditorFor(m => m[u].OneWeek, "TimesheetWeek")
<input type="Submit">
}
Views (TimesheetWeek) 意见(TimesheetWeek)
@foreach (var value in Model)
{
v++;
if (Model.All(x => x.projectId == 0))
{
@Html.DropDownListFor(p => p[v].projectId, (IEnumerable<SelectListItem>)projectList, "Select Project", new { @class = "notSelect" })
@Html.DropDownListFor(t => t[v].taskId, (IEnumerable<SelectListItem>)taskList, "Select Task", new { @class = "notSelect" })
}
else
{
if (value.projectId != 0)
{
@Html.DropDownListFor(p => p[v].projectId, (IEnumerable<SelectListItem>)projectList, new Dictionary<string, Object> { { "class", "SelectDrop" }, { "data-selectHead", value.projectId } })
@Html.DropDownListFor(t => t[v].taskId, (IEnumerable<SelectListItem>)taskList, new Dictionary<string, Object> { { "class", "SelectDrop" }, { "data-selectHead", value.taskId } })
}
}
@Html.CheckBoxFor(b => b[v].billable)
@Html.EditorFor(h => h[v].gethours, "HoursDisplay")
@value.gethours.Sum(a => a.numberOfHours)
}
View (HoursDisplay) 查看(HoursDisplay)
@for (var i = 0; i < Model.Count(); i++)
{
@Html.TextBoxFor(m => m[i].numberOfHours)
}
Model displays all the data correctly and the form data output posted is as follows: Model正确显示所有数据,发布的表单数据输出如下:
[0].OneWeek.[0].projectId:1
[0].OneWeek.[0].taskId:1
[0].OneWeek.[0].billable:true
[0].OneWeek.[0].billable:false
[0].OneWeek.[0].gethours.[0].numberOfHours:0
[0].OneWeek.[0].gethours.[1].numberOfHours:5
[0].OneWeek.[0].gethours.[2].numberOfHours:7
[0].OneWeek.[0].gethours.[3].numberOfHours:6
[0].OneWeek.[0].gethours.[4].numberOfHours:4
[0].OneWeek.[0].gethours.[5].numberOfHours:8
[0].OneWeek.[0].gethours.[6].numberOfHours:0
I thought I got the indexing right but currently get an empty Oneweek in the action. 我认为我的索引正确,但目前在行动中得到一个空的Oneweek。 What am I doing wrong? 我究竟做错了什么? Any assistance is appreciated. 任何帮助表示赞赏。 (Some repetition and HTML has been removed) (删除了一些重复和HTML)
Your prefixes are wrong. 你的前缀是错误的。 For example: 例如:
[0].OneWeek.[0].projectId:1
should be: 应该:
[0].OneWeek[0].projectId:1
You have an extra dot ( .
). 你有一个额外的点( .
)。 Read Phil Haack's article once again for the correct syntax when binding with lists. 在与列表绑定时,再次阅读Phil Haack的文章 ,了解正确的语法。
You have the same problem with the gethours.[0]
你对gethours.[0]
也有同样的问题gethours.[0]
part. 部分。
I would recommend you using the standard editor templates conventions and avoid writing any foreach loops and dealing with indexes: 我建议你使用标准的编辑器模板约定,避免编写任何foreach循环和处理索引:
~/Views/Home/Index.cshtml:
@model List<WeekCollection>
@using(Html.BeginForm())
{
@Html.EditorForModel()
<input type="Submit" />
}
~/Views/Home/EditorTemplates/WeekCollection.cshtml
: ~/Views/Home/EditorTemplates/WeekCollection.cshtml
:
@model WeekCollection
@Html.EditorFor(x => x.OneWeek)
~/Views/Home/EditorTemplates/Entry.cshtml
: ~/Views/Home/EditorTemplates/Entry.cshtml
:
@model Entry
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.