Bind Html.DropDownList with static items

asp.net-mvc, asp.net-mvc-3, html.dropdownlistfor

Solution

It is a best practice not to create the SelectList in the view. You should create it in the controller and pass it using the ViewData.

Example:

var list = new SelectList(new [] 
{
    new { ID = "1", Name = "name1" },
    new { ID = "2", Name = "name2" },
    new { ID = "3", Name = "name3" },
}, 
"ID", "Name", 1);

ViewData["list"]=list;
return View();

you pass to the constratctor: the IEnumerable objec,the value field the text field and the selected value.

in the View:

<%=Html.DropDownList("list",ViewData["list"] as SelectList) %>

Problem

I have to bind an `Html.DropDownList` with just two items statically. ``` Text="Yes" Value="1" Text="No" Value="0" ``` The important thing is that, I have to set the text and value fields. How can I do this?

Original source