为什么我的视图输入数据不像其他输入那样传递给我的控制器?

时间:2014-03-05 22:08:37

标签: asp.net ajax asp.net-mvc asp.net-mvc-4 razor

我有这个视图将IEnumerable<WeeklyTarget>对象传回控制器。我的所有输入都成功绑定到发送到控制器操作的模型,除了一个。此输入与其他输入略有不同,当用户选择页面上的按钮时,我进行ajax调用以将类型显示的原始输入替换为此属性的数字编辑器。这是ajax调用之前的表单/视图:

@using (Html.BeginForm("SaveEdit", "WeeklyTarget", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    foreach (var item in Model)
    {
        <tr>
            // this first hidden input sets the index for each item in Model      
            @Html.Hidden("weeklytargets.Index", item.WeeklyTargetId)

            // this hidden input binds perfectly, along with the other next two
            @Html.Hidden("weeklytargets[" + item.WeeklyTargetId + "].WeeklyTargetId", item.WeeklyTargetId)
            <td>
                // this binds fine also
                @Html.Hidden("weeklytargets[" + item.WeeklyTargetId + "].UserId", item.UserId)
                @Html.DisplayFor(modelItem => item.User.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.User.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.User.Email)
            </td>
            <td>
                // and lastly, this binds fine as well
                @Html.Hidden("weeklytargets[" + item.WeeklyTargetId + "].WeekId", item.WeekId)
                @item.Weekly.WeekBeginning.ToShortDateString()  -  @item.Weekly.WeekEnding.ToShortDateString()
            </td>
            <td id="hoursEdit-@item.WeeklyTargetId">
                // this display here is replaced with a different line of razor code
                // when the user selects edit, the updated code is after the jump
                @Html.DisplayFor(modelItem => item.Hours)
            </td>
            <td>
                // this edit button makes an ajax call to replace the display above
                @Ajax.ActionLink("Edit", "Index", "WeeklyTarget",
                    new {id = item.WeeklyTargetId},
                    new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "hoursEdit-"+@item.WeeklyTargetId, InsertionMode = InsertionMode.Replace }, new { @class = "edit-button", @id = item.WeeklyTargetId }) |                    
            </td>
        </tr>
    }
}

因此当编辑按钮进行ajax调用时,它会用这行代码替换@Html.DisplayFor(modelItem => item.Hours)

@Html.EditorFor(item => item.Hours, null, "weeklytargets[" + Model.WeeklyTargetId + "].Hours")

现在在chrome调试器中,我仔细检查了这些输入的名称评估的内容,以便它们正确绑定到我的模型,它们如下:

name="weeklytargets[X].WeeklyTargetId"
name="weeklytargets[X].UserId"
name="weeklytargets[X].WeekId"
name="weeklytargets[X].Hours" 

最后一个'Hours'是唯一一个在weeklytargets对象的控制器参数中显示为null的,为什么会这样?任何帮助或见解将不胜感激。

0 个答案:

没有答案