使用DropDownListFor绑定值

时间:2017-12-14 10:00:04

标签: asp.net-mvc html.dropdownlistfor

在我看来,除下拉列表外,所有字段都绑定到模型。列表已填充,但未设置所选值。

在我的控制器中,我设置了这样的数据源:

ViewBag.LocationID = new SelectList(db.Locations, "ID", "Name", model.LocationID);

在我看来,我设置了这样的下拉列表:

@Html.DropDownListFor(model => model.LocationID, (SelectList)ViewBag.LocationID, string.Empty, htmlAttributes: new { @class = "form-control" })

在我读到的有类似问题的问题中,问题是他们在创建SelectList时没有设置选定的值,但我这样做。

什么阻止所选值受模型约束?

0 个答案:

没有答案
相关问题