How do you bind an Enum to a DropDownList control in ASP.NET?

后端 未结 25 2593
既然无缘
既然无缘 2020-11-29 15:41

Let\'s say I have the following simple enum:

enum Response
{
    Yes = 1,
    No = 2,
    Maybe = 3
}

How can I bind this enum to a DropDow

25条回答
  •  时光说笑
    2020-11-29 16:18

    ASP.NET has since been updated with some more functionality, and you can now use built-in enum to dropdown.

    If you want to bind on the Enum itself, use this:

    @Html.DropDownList("response", EnumHelper.GetSelectList(typeof(Response)))
    

    If you're binding on an instance of Response, use this:

    // Assuming Model.Response is an instance of Response
    @Html.EnumDropDownListFor(m => m.Response)
    

提交回复
热议问题