如何使用C#从asp.net MVC中的枚举中绑定一个下拉列表

时间:2013-08-23 06:56:42

标签: c# asp.net-mvc

我正在尝试绑定来自Html.DropDownList的{​​{1}}。任何人都可以提前帮助我。

1 个答案:

答案 0 :(得分:0)

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" })