2012-12-23 39 views
8

这是我的第一个C#/ MVC项目,我遇到了绑定到模型的问题。我已经阅读了应用Phil Haack's Post,并使用了EditorFor作为我的所有部分视图。我需要一个自定义模型绑定器吗?请帮助MVC C#嵌套列表中的自动模型绑定

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

操作:

[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已被删除)

回答

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 
... 
+0

非常感谢你。我没有意识到EditorFor实际上是多么有用,因此视图模型非常简单。非常有用的知识。 Thanks + Merry Christmas – user1925048

+0

Entry.cshtml的代码是什么? – Apollo