How To bind A dropdownlist from enum in asp.net MVC using C#
本问题已经有最佳答案,请猛点这里访问。
我正试图从
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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" }) |