我的视图模型是:
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 帖子中的视图模型的任何原因吗?
谢谢你,马克