ASP.Net MVC发布的表单值不会映射回控制器中的viewmodel

时间:2013-08-15 14:07:22

标签: asp.net asp.net-mvc asp.net-mvc-3

我的ViewModel是:

public class ObjectiveVM
{
    public string DateSelected { get; set; }
    public List<string> DatePeriod { get; set; }
    public IList<ObList> obList { get; set; }
    public class ObList
    {
        public int ObjectiveId { get; set; }
        public int AnalystId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string AnalystName { get; set; }
        public bool Include { get; set; }
    }
}

这将传递给视图,按预期填充 - 并在视图中正确显示。

我的问题是它何时被发回控制器。我接受它的控制器代码是:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Analyst(ObjectiveVM ovm)

ovm.obList始终显示为null:

Showing posted values

My View html是:

@model Objectives.ViewModels.ObjectiveVM

@{
ViewBag.Title = "Analyst";
}
<h2>Copy Objectives for Analyst</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Objective</legend>
    @Html.DropDownListFor(model => model.DateSelected, new SelectList(Model.DatePeriod))
<table>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.obList[0].Include)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.obList[0].AnalystName)
    </th>
     <th>
        @Html.DisplayNameFor(model => model.obList[0].Title)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.obList[0].Description)
    </th>
</tr>

@foreach (var obList in Model.obList)
{
    <tr>
       <td>

            @Html.HiddenFor(modelItem => obList.ObjectiveId)
            @Html.HiddenFor(modelItem => obList.AnalystId)
            @Html.HiddenFor(modelItem => obList.Title)
            @Html.HiddenFor(modelItem => obList.Description)

            @Html.CheckBoxFor(modelItem => obList.Include)
        </td>
         <td>
            @Html.DisplayFor(modelItem => obList.AnalystName)
        </td>
         <td>
            @Html.DisplayFor(modelItem => obList.Title) 
         </td>
        <td>
            @Html.DisplayFor(modelItem => obList.Description)
        </td>
    </tr>
}

</table>

    <p>
        <input type="submit" value="Copy Selected Objectives" />
    </p>
</fieldset>

    }

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

在发布表单值中查看开发人员工具,它们似乎没问题: Developer Tools

任何人都可以看到发布的表单值的任何原因,不会映射回Controller HTTP帖子中的viewmodel吗?

谢谢Mark,

2 个答案:

答案 0 :(得分:2)

您需要在此使用for...loop,而不是foreach....loop

@for (int idx = 0;idx < Model.obList.Count;idx++){
    @Html.HiddenFor(_ => Model.obList[idx].ObjectiveId)

    // ... etc....

}

如果没有索引器(idx),模型绑定器将不知道如何将值绑定回正确的集合项。

答案 1 :(得分:1)

在我的视图中使用集合时,我通常会在不使用帮助程序的情况下写出我的标记:

@for (int i = 0;i < Model.obList.Count();i++){
     <input type="hidden" name="ObList[@i].ObjectiveId" id="ObList[@i].ObjectiveId" value="@ObList[i].ObjectiveId" />
     <input type="hidden" name="ObList[@i].AnalystId" id="ObList[@i].AnalystId" value="@ObList[i].AnalystId" />
     ...
}

这将符合模型绑定器所期望的有线格式,并将您的值插入ViewModel:http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx