I am trying to bind a Html.DropDownList
from Enum
. Can anyone help me thanks in advance.
Asked
Active
Viewed 3,927 times
0

tereško
- 58,060
- 25
- 98
- 150

joinabhinav
- 21
- 1
- 2
-
Did you try anything? Show your code.. – Soner Gönül Aug 23 '13 at 06:57
-
a simple search would have done it :http://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc – Zaki Aug 23 '13 at 06:59
-
I have done this code here you can get help from this. [http://stackoverflow.com/questions/18380576/bind-enum-with-dropdown-and-set-selected-value-on-get-action-in-mvc-c-sharp] – Satish Singh Aug 23 '13 at 06:59
1 Answers
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" })

Satish Singh
- 2,169
- 3
- 23
- 32