This post will help you to bind a dropdownlist from an Enum in MVC 3 application. Here i create a methode which will return an IQueryable<SelectListItem> list from a given enum.
the methode is like bellow
public static IQueryable<SelectListItem> BindDropdownListFromEnum<T>()
{
List<SelectListItem> lstEnum = new List<SelectListItem>();
var lsttype = from t in lstEnum select t;
try
{
T objType = Activator.CreateInstance<T>();
foreach (T enumValue in Enum.GetValues(typeof(T)))
{
FieldInfo fi = typeof(T).GetField(enumValue.ToString());
DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
SelectListItem lstItem = new SelectListItem();
int val = (int)fi.GetValue(enumValue);
if (da != null)
{
lstItem = new SelectListItem { Text = da.Description, Value = Convert.ToString(val) };
}
else
{
lstItem = new SelectListItem { Text = fi.Name, Value = Convert.ToString(val) };
}
if (!lstEnum.Contains(lstItem))
{
lstEnum.Add(lstItem);
}
}
lsttype = from t in lstEnum select t;
}
catch (Exception ex)
{
throw ex;
}
return ((IEnumerable<SelectListItem>)lsttype).Select(x => x).AsQueryable();
}
Here if the enum has description for its Key then it will return the Key as Description and the Value will be the enum value. as a list of IEnumerable<SelectListItem> where SelectListItem's key and value will be the enum types key and value respectively.
In controller
-----------------
IEnumerable<SelectListItem> lstUsertypes = EnumUtility.BindDropdownListFromEnum<UserType>();
ViewBag.UserType = lstUsertypes;
In view
----------
Usertype : @Html.DropDownList("UserType", "-- Select All --")
Enum
-----------
/// <summary>
/// Enumeration UserType use for Get User Type
/// </summary>
public enum UserType
{
/// <summary>
/// For Private Value is 1
/// </summary>
Private = 1,
/// <summary>
/// For Company Value is 2
/// </summary>
Company = 2,
/// <summary>
/// For Organisation Value is 3
/// </summary>
Organisation = 3
}
the methode is like bellow
public static IQueryable<SelectListItem> BindDropdownListFromEnum<T>()
{
List<SelectListItem> lstEnum = new List<SelectListItem>();
var lsttype = from t in lstEnum select t;
try
{
T objType = Activator.CreateInstance<T>();
foreach (T enumValue in Enum.GetValues(typeof(T)))
{
FieldInfo fi = typeof(T).GetField(enumValue.ToString());
DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
SelectListItem lstItem = new SelectListItem();
int val = (int)fi.GetValue(enumValue);
if (da != null)
{
lstItem = new SelectListItem { Text = da.Description, Value = Convert.ToString(val) };
}
else
{
lstItem = new SelectListItem { Text = fi.Name, Value = Convert.ToString(val) };
}
if (!lstEnum.Contains(lstItem))
{
lstEnum.Add(lstItem);
}
}
lsttype = from t in lstEnum select t;
}
catch (Exception ex)
{
throw ex;
}
return ((IEnumerable<SelectListItem>)lsttype).Select(x => x).AsQueryable();
}
Here if the enum has description for its Key then it will return the Key as Description and the Value will be the enum value. as a list of IEnumerable<SelectListItem> where SelectListItem's key and value will be the enum types key and value respectively.
In controller
-----------------
IEnumerable<SelectListItem> lstUsertypes = EnumUtility.BindDropdownListFromEnum<UserType>();
ViewBag.UserType = lstUsertypes;
In view
----------
Usertype : @Html.DropDownList("UserType", "-- Select All --")
Enum
-----------
/// <summary>
/// Enumeration UserType use for Get User Type
/// </summary>
public enum UserType
{
/// <summary>
/// For Private Value is 1
/// </summary>
Private = 1,
/// <summary>
/// For Company Value is 2
/// </summary>
Company = 2,
/// <summary>
/// For Organisation Value is 3
/// </summary>
Organisation = 3
}
No comments:
Post a Comment