How to create a static dropdown in Razor syntax?

After hours of searching on Google and overflowing the stack, I can't find one bloody example of how to create a complete drop-down list of a completely dead brain that doesn't come from a database. Honestly, it's hard for me to catch my head around MVC. Can someone please show me how to do this:

<select name="FooBarDropDown" id="FooBarDropDown"> <option value="Option1" selected>This is Option 1</option> <option value="Option2">This is Option 2</option> <option value="Option3">This is Option 3</option> </select> 

Using this:

 @Html.DropDownList.... 

I'm looking for an all-in-one solution ... all in a view. I have a time devil with syntax.

+13
asp.net-mvc html.dropdownlistfor
source share
2 answers

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")) 
+30
source share

See the docs for this overload.

 public static MvcHtmlString DropDownList( this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList ) 

So just add a link to your List<SelectListItem>() with your parameters.

 List<SelectListItem> items = new List<SelectListItem>(); items.Add(new SelectListItem { Text = "Option1", Value = "Option1"}); items.Add(new SelectListItem { Text = "Option2", Value = "Option2" }); items.Add(new SelectListItem { Text = "Option3", Value = "Option3", Selected = true }); 

You can even insert this into your opinion if you do not want to transfer it from your controller.

 @{ List<SelectListItem> items = ... } 

Then use it

 @Html.DropDownList("FooBarDropDown", items) 
+3
source share

All Articles