static public List<SelectListItem> ToListItem<T>()
{
List<SelectListItem> li = new List<SelectListItem>();
dynamic type = typeof(T);
string a=type.ToString();
foreach (int s in Enum.GetValues(typeof(T)))
{
li.Add(new SelectListItem
{
Value = s.ToString(),
Text = GetEnumDescription(Enum.ToObject(typeof(T), s))
});
}
return li;
}
public static string GetEnumDescription(object enumSubitem)
{
string strValue = enumSubitem.ToString();
FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue);
Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs == null || objs.Length == 0)
{
return strValue;
}
else
{
DescriptionAttribute da = (DescriptionAttribute)objs[0];
return da.Description;
}
}
string dropDown = Request["dropDown"];
string selectValue = string.IsNullOrEmpty(dropDown) ? "0" : dropDown;
var dropDownListPre = new SelectList(EnumExt.ToListItem<RedRoomCardOperateType>(), "value", "text");
List<SelectListItem> dropDownListItems = (List<SelectListItem>)dropDownListPre.Items;
dropDownListItems.Add(new SelectListItem { Value = "0", Text = "请选择" });
var m = (from tb in dropDownListItems where tb.Value == selectValue select tb).FirstOrDefault();
m.Selected = true; m.Disabled = false;
ViewBag.dropDownList = dropDownListItems;
@Html.DropDownList("dropDown", @ViewBag.dropDownList as List<SelectListItem>)