I am trying to bind a Html.DropDownList from Enum. Can anyone help me thanks in advance.
-
Did you try anything? Show your code..Soner Gönül– Soner Gönül2013-08-23 06:57:10 +00:00Commented Aug 23, 2013 at 6:57
-
a simple search would have done it :stackoverflow.com/questions/388483/…Zaki– Zaki2013-08-23 06:59:02 +00:00Commented Aug 23, 2013 at 6:59
-
I have done this code here you can get help from this. [stackoverflow.com/questions/18380576/…Satish Singh– Satish Singh2013-08-23 06:59:04 +00:00Commented Aug 23, 2013 at 6:59
Add a comment
|
1 Answer
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" })