【发布时间】:2016-09-23 14:09:58
【问题描述】:
我有几个这样声明的多选列表的视图
<div class="col-md-6">
@Html.LabelFor(model => model.Counties, htmlAttributes: new { @class = "control-label" })
@Html.ListBoxFor(model => model.Counties, new MultiSelectList(ViewBag.CountyList, "Value", "Text"), htmlAttributes: new { @class = "form-control", size = 8, tabindex = 26 })
@Html.ValidationMessageFor(model => model.Counties, "", new { @class = "text-danger" })
<span class="small">Ctrl + click to select multiple items</span>
</div>
我的视图模型包含这样的声明:
public virtual List<long> Counties { get; protected set; }
我的动作是这样的:
[HttpPost]
public ActionResult Edit(TScholarshipView model, FormCollection form)
{
if (ModelState.IsValid)
{
TScholarship scholarship = Repo.GetScholarship(model.Id);
scholarship = Mapper.Map<TScholarshipView, TScholarship>(model, scholarship);
Repo.SaveOrUpdate(scholarship, HttpContext.User.Identity.Name);
return RedirectToAction("Edit", "AdminScholarship", new { id = model.Id });
}
return View("Scholarship", model);
}
在提交时,我可以查看浏览器发送的发布数据,它正在向服务器发送适当的数据
...&Counties=16&Counties=34&...
当动作开始执行时,模型中 Counties 的值为 null。但是我可以查看 FormCollection 的值,而 form["Counties"] 的值是 "16,34"。任何想法为什么绑定没有发生?
【问题讨论】:
标签: model-binding asp.net-mvc-5 html.listboxfor