DropDownList项的值

pal*_*now 1 asp.net-mvc-3

我有以下代码

@Html.DropDownList("optionsforuser", new SelectList(new[] { "Option1", "Option2", "Option3" }), "Select")
Run Code Online (Sandbox Code Playgroud)

无论如何,在SelectList中为Option1初始化值100,为Option2初始化200,为Option3等初始化250?

hun*_*ter 5

尝试使用此扩展:

@Html.DropDownList("optionsforuser", 
    new SelectList(new Dictionary<string, int> 
        {
            {"Option1", 100},
            {"Option2", 200},
            {"Option3", 250}
        },
        "Value", "Key")
)
Run Code Online (Sandbox Code Playgroud)

Dictionary<string, int>使用dataValueField和dataTextField 传递它,并使用您的值和文本填充


SelectExtensions.DropDownList方法(HtmlHelper,String,IEnumerable(Of SelectListItem))

public static MvcHtmlString DropDownList(
    this HtmlHelper htmlHelper,
    string name,
    IEnumerable<SelectListItem> selectList
)
Run Code Online (Sandbox Code Playgroud)

SelectList构造函数(IEnumerable,String,String)

public SelectList(
    IEnumerable items,
    string dataValueField,
    string dataTextField
)
Run Code Online (Sandbox Code Playgroud)