In ASP.NET MVC, you can create a dropdown list from an enum by following these steps:
1. Define the enum in your C# code:
public enum MyEnum { Option1, Option2, Option3 }
2. In your controller, create a SelectList
from the enum values:
var enumValues = Enum.GetValues(typeof(MyEnum)) .Cast<MyEnum>() .Select(e => new SelectListItem { Text = e.ToString(), Value = ((int)e).ToString() }); ViewBag.EnumValues = enumValues;
3. In your view, use the Html.DropDownListFor
helper method to create a dropdown list:
@Html.DropDownListFor(m => m.MyEnumProperty, (IEnumerable<SelectListItem>)ViewBag.EnumValues, "Select an option")
In this example, MyEnumProperty
is a property on your model that represents the selected enum value. The IEnumerable<SelectListItem>
cast is necessary because ViewBag.EnumValues
is an IEnumerable
of a custom type.
The third argument to Html.DropDownListFor
is the option label that is displayed as the first item in the dropdown list. You can set it to an empty string if you don't want an option label.
When the form is submitted, the selected enum value will be bound to the MyEnumProperty
property on your model.
"Create dropdownlist for enum in ASP.NET MVC"
public enum MyEnum { Value1, Value2, Value3 }; public ActionResult Index() { ViewBag.EnumValues = new SelectList(Enum.GetValues(typeof(MyEnum))); return View(); }
MyEnum
enum."Bind enum values to dropdownlist in ASP.NET MVC"
public ActionResult Index() { ViewBag.EnumValues = new SelectList(Enum.GetValues(typeof(MyEnum)), "Value"); return View(); }
"Dropdownlistfor enum in ASP.NET MVC Razor View"
@Html.DropDownListFor(model => model.MyEnumProperty, new SelectList(Enum.GetValues(typeof(MyEnum))))
MyEnum
."Display enum descriptions in dropdownlist in ASP.NET MVC"
public enum MyEnum { [Description("Custom Value 1")] Value1, [Description("Custom Value 2")] Value2, [Description("Custom Value 3")] Value3 };
ViewBag.EnumValues = new SelectList(EnumHelper.GetEnumDescriptions<MyEnum>());
Description
attributes for enum values and a helper method to get descriptions for the dropdownlist."How to handle enum selection in ASP.NET MVC controller"
[HttpPost] public ActionResult Index(MyEnum selectedValue) { // Handle selected enum value in the controller return View(); }
"Create enum dropdownlist with selected value in ASP.NET MVC"
ViewBag.EnumValues = new SelectList(Enum.GetValues(typeof(MyEnum)), selectedValue);
selectedValue
parameter."Use SelectListItems for enum dropdownlist in ASP.NET MVC"
ViewBag.EnumValues = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().Select(e => new SelectListItem { Text = e.ToString(), Value = ((int)e).ToString() });
SelectListItem
objects for better control over the dropdownlist."Bind enum values with custom names to dropdownlist in ASP.NET MVC"
ViewBag.EnumValues = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().Select(e => new SelectListItem { Text = GetCustomNameForEnumValue(e), Value = e.ToString() });
"Dynamic enum dropdownlist in ASP.NET MVC"
public ActionResult Index(string enumTypeName) { Type enumType = Type.GetType(enumTypeName); ViewBag.EnumValues = new SelectList(Enum.GetValues(enumType)); return View(); }
"Use EnumDisplayName attribute in dropdownlist in ASP.NET MVC"
ViewBag.EnumValues = new SelectList(Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().Select(e => new SelectListItem { Text = e.GetEnumDisplayName(), Value = e.ToString() }));
EnumDisplayName
) to get display names for enum values in the dropdownlist.fft viewflipper routerlink tesseract azure-storage beagleboneblack window-size asp.net-identity-2 android-viewpager2 docker-machine