如何在ASP.NET MVC Web应用程序中创建一个基于枚举的下拉列表框,有以下几种方式供参考:
- 在ASP.NET MVC 5.1中,可以直接使用Html.EnumDropDownListFor来实现,参考代码:
@Html.EnumDropDownListFor(
x => x.YourEnumField,
"Select My Type",
new { @class = "form-control" })
- 在ASP.NET MVC 5中,可以使用EnumHelper类来处理,参考代码:
@Html.DropDownList("MyType",
EnumHelper.GetSelectList(typeof(MyType)) ,
"Select My Type",
new { @class = "form-control" })
- 在ASP.NET MVC 5以前的版本中,可以自己实现一个扩展类来处理,参考代码:
public static class EnumEditorHtmlHelper
{
/// <summary>
/// Creates the DropDown List (HTML Select Element) from LINQ
/// Expression where the expression returns an Enum type.
/// </summary>
/// <typeparam name="TModel">The type of the model.</typeparam>
/// <typeparam name="TProperty">The type of the property.</typeparam>
/// <param name="htmlHelper">The HTML helper.</param>
/// <param name="expression">The expression.</param>
/// <returns></returns>
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression)
where TModel : class
{
TProperty value = htmlHelper.ViewData.Model == null
? default(TProperty)
: expression.Compile()(htmlHelper.ViewData.Model);
string selected = value == null ? String.Empty : value.ToString();
return htmlHelper.DropDownListFor(expression, createSelectList(expression.ReturnType, selected));
}
/// <summary>
/// Creates the select list.
/// </summary>
/// <param name="enumType">Type of the enum.</param>
/// <param name="selectedItem">The selected item.</param>
/// <returns></returns>
private static IEnumerable<SelectListItem> createSelectList(Type enumType, string selectedItem)
{
return (from object item in Enum.GetValues(enumType)
let fi = enumType.GetField(item.ToString())
let attribute = fi.GetCustomAttributes(typeof (DescriptionAttribute), true).FirstOrDefault()
let title = attribute == null ? item.ToString() : ((DescriptionAttribute) attribute).Description
select new SelectListItem
{
Value = item.ToString(),
Text = title,
Selected = selectedItem == item.ToString()
}).ToList();
}
public static SelectList ToSelectList(this TEnum enumObj,
bool markCurrentAsSelected = true, int[] valuesToExclude = null, bool useLocalization = true) where TEnum : struct
{
if (!typeof(TEnum).IsEnum) throw new ArgumentException("An Enumeration type is required.", "enumObj");
var localizationService = EngineContext.Current.Resolve();
var workContext = EngineContext.Current.Resolve();
var values = from TEnum enumValue in Enum.GetValues(typeof(TEnum))
where valuesToExclude == null || !valuesToExclude.Contains(Convert.ToInt32(enumValue))
select new { ID = Convert.ToInt32(enumValue), Name = useLocalization ? enumValue.GetLocalizedEnum(localizationService, workContext) : CommonHelper.ConvertEnum(enumValue.ToString()) };
object selectedValue = null;
if (markCurrentAsSelected)
selectedValue = Convert.ToInt32(enumObj);
return new SelectList(values, "ID", "Name", selectedValue);
}
public static SelectList ToSelectList(this T objList, Func<BaseEntity, string> selector) where T : IEnumerable
{
return new SelectList(objList.Select(p => new { ID = p.Id, Name = selector(p) }), "ID", "Name");
}
}
使用方式与其他的Html标签类似,直接Html.DropDownListFor(m=>m.EnumItems),其中EnumItems是你的枚举
版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。
发表评论
登录用户才能发表评论, 请 登 录 或者 注册