1

我的视图模型是:

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始终显示为空:

显示过帐值

我的视图 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")
}

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

任何人都可以看到发布的表单值没有映射回控制器 HTTP 帖子中的视图模型的任何原因吗?

谢谢你,马克

4

2 回答 2

2

你需要在for...loop这里使用 a ,而不是 a foreach....loop

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

    // ... etc....

}

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

于 2013-08-15T14:11:56.900 回答
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

于 2013-08-15T14:23:24.060 回答