MVC C#嵌套列表中的自动模型绑定

时间:2012-12-23 15:51:25

标签: c# asp.net-mvc model-binding

这是我的第一个C#/ MVC项目,我遇到了与模型绑定的问题。我已经阅读了应用Phil Haack's Post并使用了EditorFor来获取所有部分视图。我需要自定义型号粘合剂吗?请帮忙

简而言之,我有一个包含条目列表的周列表。这些条目包含小时列表

动作:

[HttpPost]
public ActionResult SubmitRecords(List<WeekCollection> itemData)
{
     //do stuff
     return View();
}

型号:

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

观点(索引)

//within partial view iteration with incrementing u (u++)
@using(@Html.BeginForm())
{ 
    @Html.EditorFor(m => m[u].OneWeek, "TimesheetWeek")
    <input type="Submit">
}

观点(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)
     }

查看(HoursDisplay)

@for (var i = 0; i < Model.Count(); i++)
{
    @Html.TextBoxFor(m => m[i].numberOfHours)
}

模型正确显示所有数据,发布的表单数据输出如下:

[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

我认为我的索引正确,但目前在行动中得到一个空的Oneweek。我究竟做错了什么?任何帮助表示赞赏。 (删除了一些重复和HTML)

1 个答案:

答案 0 :(得分:7)

你的前缀错了。例如:

[0].OneWeek.[0].projectId:1

应该是:

[0].OneWeek[0].projectId:1

你有一个额外的点(.)。在使用列表绑定时,再次阅读Phil Haack's article以获取正确的语法。

gethours.[0]部分存在同样的问题。

我建议您使用标准编辑器模板约定,避免编写任何foreach循环并处理索引:

~/Views/Home/Index.cshtml:

@model List<WeekCollection>
@using(Html.BeginForm())
{ 
    @Html.EditorForModel()
    <input type="Submit" />
}

~/Views/Home/EditorTemplates/WeekCollection.cshtml

@model WeekCollection
@Html.EditorFor(x => x.OneWeek)

~/Views/Home/EditorTemplates/Entry.cshtml

@model Entry
...