Create DropDownList with an enum in ASP.NET MVC

  • How to create a dropdownlist from an enum in ASP.NET MVC:

You can use the Html.EnumDropDownListFor method to create a dropdownlist from an enum in ASP.NET MVC. Here is an example:

@model MyModel
@{
    ViewBag.Title = "My Page";
}
@using (Html.BeginForm("MyAction", "MyController", FormMethod.Post))
{
    @Html.EnumDropDownListFor(m => m.MyEnumProperty, "Please select an item")
    <input type="submit" value="Submit" />
}

In the above example, MyModel is the model class and MyEnumProperty is the enum property to bind the selected value to. The EnumDropDownListFor method will generate a dropdownlist from the values of the MyEnumProperty enum.

  • How to create a SelectList from an enum in ASP.NET MVC:

You can use the Enum.GetValues method to create a SelectList from an enum in ASP.NET MVC. Here is an example:

public ActionResult MyAction()
{
    var values = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().Select(e => new SelectListItem
    {
        Text = e.ToString(),
        Value = ((int)e).ToString()
    });
    var model = new MyModel
    {
        MyEnumPropertyList = new SelectList(values, "Value", "Text")
    };
    return View(model);
}

In the above example, MyEnum is the enum type, and we use the Enum.GetValues method to get all the values of the enum. We then cast the values to the MyEnum type and create a SelectListItem for each value. Finally, we create a new SelectList from the SelectListItem objects, and set the MyEnumPropertyList property of the model to the SelectList. You can then use the MyEnumPropertyList property in the view to generate a dropdownlist.

  1. ASP.NET MVC DropDownList with Enum Example:

    Create a DropDownList in ASP.NET MVC using an enum.

    // In your controller
    ViewBag.EnumValues = Enum.GetValues(typeof(YourEnum));
    
    <!-- In your view -->
    @Html.DropDownList("SelectedEnumValue", new SelectList(ViewBag.EnumValues))
    
  2. Bind Enum to DropDownList in MVC:

    Bind an enum to a DropDownList in ASP.NET MVC.

    // In your controller
    ViewBag.EnumValues = Enum.GetValues(typeof(YourEnum));
    
    <!-- In your view -->
    @Html.DropDownList("SelectedEnumValue", new SelectList(ViewBag.EnumValues))
    
  3. DropDownListFor with Enum in ASP.NET MVC:

    Use DropDownListFor with an enum in ASP.NET MVC.

    // In your model
    public YourEnum SelectedEnumValue { get; set; }
    
    // In your view
    @Html.DropDownListFor(model => model.SelectedEnumValue, new SelectList(Enum.GetValues(typeof(YourEnum))))
    
  4. Populate DropDownList from Enum in MVC:

    Populate a DropDownList with enum values in ASP.NET MVC.

    // In your controller
    ViewBag.EnumValues = Enum.GetValues(typeof(YourEnum));
    
    <!-- In your view -->
    @Html.DropDownList("SelectedEnumValue", new SelectList(ViewBag.EnumValues))
    
  5. Create a Dropdown List from an Enum in ASP.NET MVC:

    Create a dropdown list in ASP.NET MVC using an enum.

    // In your controller
    ViewBag.EnumValues = Enum.GetValues(typeof(YourEnum));
    
    <!-- In your view -->
    @Html.DropDownList("SelectedEnumValue", new SelectList(ViewBag.EnumValues))
    
  6. Using Enum as a Data Source for DropDownList in MVC:

    Utilize an enum as a data source for a DropDownList in ASP.NET MVC.

    // In your controller
    ViewBag.EnumValues = Enum.GetValues(typeof(YourEnum));
    
    <!-- In your view -->
    @Html.DropDownList("SelectedEnumValue", new SelectList(ViewBag.EnumValues))
    
  7. Enum to DropDownList Binding in ASP.NET MVC:

    Bind an enum to a DropDownList in ASP.NET MVC.

    // In your controller
    ViewBag.EnumValues = Enum.GetValues(typeof(YourEnum));
    
    <!-- In your view -->
    @Html.DropDownList("SelectedEnumValue", new SelectList(ViewBag.EnumValues))
    
  8. ASP.NET MVC EnumDropDownListFor Example:

    Use EnumDropDownListFor to bind an enum to a DropDownList in ASP.NET MVC.

    // In your model
    public YourEnum SelectedEnumValue { get; set; }
    
    // In your view
    @Html.EnumDropDownListFor(model => model.SelectedEnumValue)
    
  9. Enum SelectList in DropDownList ASP.NET MVC:

    Use SelectList with an enum to create a DropDownList in ASP.NET MVC.

    // In your controller
    ViewBag.EnumValues = new SelectList(Enum.GetValues(typeof(YourEnum)));
    
    <!-- In your view -->
    @Html.DropDownList("SelectedEnumValue", ViewBag.EnumValues)
    
  10. Dropdown List for Enum Values in ASP.NET MVC:

    Create a dropdown list for enum values in ASP.NET MVC.

    // In your controller
    ViewBag.EnumValues = Enum.GetValues(typeof(YourEnum));
    
    <!-- In your view -->
    @Html.DropDownList("SelectedEnumValue", new SelectList(ViewBag.EnumValues))