如何使用C#从asp.net MVC中的enum绑定dropdownlist
本文关键字:中的 enum 绑定 dropdownlist MVC net 何使用 asp | 更新日期: 2023-09-27 18:28:47
我正在尝试从Enum
绑定Html.DropDownList
。有人能帮我吗?提前谢谢。
public enum CityType
{
[Description("Select City")]
Select = 0,
[Description("A")]
NewDelhi = 1,
[Description("B")]
Mumbai = 2,
[Description("C")]
Bangalore = 3,
[Description("D")]
Buxar = 4,
[Description("E")]
Jabalpur = 5
}
IList<SelectListItem> list = Enum.GetValues(typeof(CityType)).Cast<CityType>().Select(x => new SelectListItem(){
Text = EnumHelper.GetDescription(x),
Value = ((int)x).ToString()
}).ToList();
int city=0;
if (userModel.HomeCity != null) city= (int)userModel.HomeCity;
ViewData["HomeCity"] = new SelectList(list, "Value", "Text", city);
@Html.DropDownList("HomeCity",null,new { @style = "width:155px;", @class = "form-control" })