I think this is what you are looking for. It would be better if refactoring built the list into a view model or in a controller.
@Html.DropDownList("FooBarDropDown", new List<SelectListItem> { new SelectListItem{ Text="Option 1", Value = "1" }, new SelectListItem{ Text="Option 2", Value = "2" }, new SelectListItem{ Text="Option 3", Value = "3" }, }
An example of placing this in the controller might look like this:
public ActionResult ExampleView() { var list = new List<SelectListItem> { new SelectListItem{ Text="Option 1", Value = "1" }, new SelectListItem{ Text="Option 2", Value = "2" }, new SelectListItem{ Text="Option 3", Value = "3", Selected = true }, }); ViewData["foorBarList"] = list; return View(); }
And then, in your opinion:
@Html.DropDownList("fooBarDropDown", ViewData["list"] as List<SelectListItem>)
If this is a truly static list that you might have to reuse in other views / controllers, then I would like to consider putting this logic into a static class. Example:
public static class DropDownListUtility { public static IEnumerable<SelectListItem> GetFooBarDropDown(object selectedValue) { return new List<SelectListItem> { new SelectListItem{ Text="Option 1", Value = "1", Selected = "1" == selectedValue.ToString()}, new SelectListItem{ Text="Option 2", Value = "2", Selected = "2" == selectedValue.ToString()}, new SelectListItem{ Text="Option 3", Value = "3", Selected = "3" == selectedValue.ToString()}, }; }
Which then leaves you with several different ways to access the list.
Controller example:
public ActionResult ExampleView() { var list = DropDownListUtility.GetFooBarDropDown("2"); //select second option by default; ViewData["foorBarList"] = list; return View(); }
See an example:
@Html.DropDownList("fooBarDropDown", DropDownListUtility.GetFooBarDropDown("2"))