如何在ASp.Net Mvc3中使用Razor从IDictionary创建DropDown列表?我在尝试以下代码时没有成功。
public IDictionary<string, string> CandidatesList = new Dictionary<string, string>();
Html.DropDownListFor(modal => modal.CandidatesList, new SelectList(Model.CandidatesList, "Value", "Key"))
答案 0 :(得分:1)
不要将下拉列表绑定到与第二个参数相同的属性。您必须将其绑定到模型上的基本类型属性:
@Html.DropDownListFor(
model => model.SelectedCandidateKey,
new SelectList(Model.CandidatesList, "Value", "Key")
)
其中SelectedCandidateKey
必须是视图模型上的字符串属性,该属性将保存所选项目键。
以这种方式思考:当您需要ASP.NET MVC中的下拉列表时,您必须在视图模型上声明2个属性:
IEnumerable<SelectListItem>
属性