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

后端 未结 25 2587
既然无缘
既然无缘 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:10

    I use this for ASP.NET MVC:

    Html.DropDownListFor(o => o.EnumProperty, Enum.GetValues(typeof(enumtype)).Cast().Select(x => new SelectListItem { Text = x.ToString(), Value = ((int)x).ToString() }))
    

提交回复
热议问题