locked
How can I create a dropdownlist from Enum in MVC? RRS feed

  • Question

  • User1974860899 posted

    Hi all,

    In my mvc project I'm trying to use the Html.DropDownList extension method but manage to use it with an enumeration. 

    Any suggestion please? Thanks

    Saturday, January 12, 2019 8:41 AM

Answers

  • User-552477072 posted

    Sample enum

    public enum ItemTypes
    {
        Movie = 1,
        Game = 2,
        Book = 3
    }

    Then call in your controller

    View code will be 

    @Html.EnumDropDownListFor(
        x => x.YourEnumField,
        "Select My Type", 
        new { @class = "form-control" })

    Please don't forget to mark as answer if it helps you. Thanks!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 12, 2019 8:48 AM