I’m learning MVC3 and I want my drop down list to use colors as the data here. How can I do it?
I know that I can do it with @Html.DropDownList("colors") but I’m wondering how to do this with @Html.DropDownListFor(....)? I’m a bit stumped and any help plus explanation would be appreciated.
I’m putting it all in one page just for convenience, so this isn’t real-world app here.
@functions {
private class Colors
{
public int ColorsId { get; set; }
public string ColorsName { get; set; }
}
}
@{
var list = new List<Colors>()
{
new Colors() {ColorsId = 1, ColorsName = "Red"},
new Colors() {ColorsId = 2, ColorsName = "Blue"},
new Colors() {ColorsId = 3, ColorsName = "White"}
};
var colors = new SelectList(list, "ColorsId", "ColorsName", 3);
}
@Html.DropDownListFor( ??? )
1 Answer