0
我想从字典{字符串,字符串}中列出MVC 2中的下拉列表。 我的代码如下,这是不完全正确的...ASP.NET MVC 2 - 从字典中填充下拉列表
states是词典。帮助
<tr>
<td class="formtext">
<%: Html.LabelFor(m => m.State)%> <strong>*</strong>
</td>
<td align="left">
<%= Html.DropDownList("statesDropDown", null, null, new { @class = "ddlAttributeGroups" })%>
</td>
</tr>
的ActionResult:
var states = new States().GetStates();
var statesDropDown = new SelectList(states, states.Keys.ToString(), states.Values.ToString());
ViewData["statesDropDown"] = statesDropDown;
我很喜欢这个,但是你会用什么建议来代替国家字典呢?私人字典 StatesDictionary = new Dictionary (){“AL”,“Alabama”}, {“AK”,“Alaska”}, {“AZ” },... –
Mark
2011-06-14 21:46:28
@Mark,我建议你在你的视图模型上使用IEnumerable,在你的域模型上使用IEnumerable 。 –
2011-06-14 21:50:41
x => x.SelectedState部分不起作用,它说:“不能将lambda表达式转换为类型'string',因为它不是委托类型” – Mark 2011-06-15 16:12:22