我想创建它,但为显示的选项添加了不同的值:
<%=Html.DropDownList("", new SelectList(new[] { "Alabama", "Alaska", "American Samoa", "Arizona", "Arkansas", "California", "Colorado", "Connecticut" }))%>
IE。将显示阿拉巴马州,但此选择的值将是AL。而且我不想使用ViewData。
答案 0 :(得分:5)
<%=
Html.DropDownList("state", new [] {
new SelectListItem() {Text = "Alabama", Value = "AL"},
new SelectListItem() {Text = "Alaska", Value = "AK" }
}) %>
答案 1 :(得分:0)
这样的东西?:
<%= Html.DropDownList("state", new[] { "Alabama,AL", "Alaska,??", "American Samoa,??", "Arizona,??", "Arkansas,??", "California,??", "Colorado,??", "Connecticut,??" }
.Select(x => new SelectListItem {
Text = x.Split(',')[0],
Value = x.Split(',')[1],
Selected = x.Split(',')[0] == "Alabama"
})
) %>