8

这是我的第一个 C#/MVC 项目,我在绑定到模型时遇到问题。我已经阅读了一篇应用的 Phil Haack 的帖子,并已将 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">
}

观看次数(时间表周)

@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)
     }

查看(小时显示)

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

4

1 回答 1

7

你的前缀是错误的。例如:

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

应该:

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

您有一个额外的点 ( .)。再次阅读Phil Haack 的文章,了解与列表绑定时的正确语法。

您对零件有同样的问题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
...
于 2012-12-23T15:55:12.337 回答