具有Option和Value的Html.DropDownList设置为不使用ViewData

时间:2009-08-02 16:48:30

标签: asp.net-mvc

我想创建它,但为显示的选项添加了不同的值:

<%=Html.DropDownList("", new SelectList(new[] { "Alabama", "Alaska", "American Samoa", "Arizona", "Arkansas", "California", "Colorado", "Connecticut" }))%>

IE。将显示阿拉巴马州,但此选择的值将是AL。而且我不想使用ViewData。

2 个答案:

答案 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"
    })
) %>