Asp.net mvc 为下拉列表向自定义html帮助器添加属性
我在这里实施了解决方案: 它只是从枚举成员创建一个下拉列表。但是,我想将一个名称与下拉列表相关联,如下所示:Asp.net mvc 为下拉列表向自定义html帮助器添加属性,asp.net-mvc,enums,extension-methods,Asp.net Mvc,Enums,Extension Methods,我在这里实施了解决方案: 它只是从枚举成员创建一个下拉列表。但是,我想将一个名称与下拉列表相关联,如下所示: @Html.TextBox(“username”,Model.username,new{id=“username”}) 为了举例说明,我想用这样一种方式来使用它: @Html.DropDownListFor(m=>m.FavoriteColor,新{id=“username”}) 我该怎么做?如何扩展EnumEditorHtmlHelper类的扩展方法 以下是EnumEditorHtm
@Html.TextBox(“username”,Model.username,new{id=“username”})
为了举例说明,我想用这样一种方式来使用它:
@Html.DropDownListFor(m=>m.FavoriteColor,新{id=“username”})
我该怎么做?如何扩展EnumEditorHtmlHelper类的扩展方法
以下是EnumEditorHtmlHelper的定义:
namespace LojmanMVC.WebUI.Static
{
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 MyEnumDropDownListFor<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();
}
}
名称空间LojmanMVC.WebUI.Static
{
公共静态类EnumEditorHtmlHelper
{
///
///从LINQ创建下拉列表(HTML选择元素)
///表达式,其中表达式返回枚举类型。
///
///模型的类型。
///属性的类型。
///HTML助手。
///表情。
///
公共静态MvcHtmlString MyEnumDropDownListFor(此HtmlHelper HtmlHelper,表达式)
其中TModel:class
{
TProperty值=htmlHelper.ViewData.Model==null
?默认值(t属性)
:expression.Compile()(htmlHelper.ViewData.Model);
所选字符串=value==null?字符串。空:value.ToString();
返回htmlHelper.DropDownListFor(表达式,createSelectList(表达式.ReturnType,selected));
}
///
///创建选择列表。
///
///枚举的类型。
///选定的项目。
///
私有静态IEnumerable createSelectList(类型enumType,字符串selectedItem)
{
返回(来自Enum.GetValues(enumType)中的对象项)
设fi=enumType.GetField(item.ToString())
let attribute=fi.GetCustomAttributes(typeof(DescriptionAttribute),true)
让title=attribute==null?item.ToString():((DescriptionAttribute)属性)。说明
选择新的SelectListItem
{
Value=item.ToString(),
文本=标题,
Selected=selectedItem==item.ToString()
}).ToList();
}
}
}
提前感谢。您可以使用相同的方法执行此操作,但它需要
第二个参数
作为SelectListItems
的集合,第三个参数
用于您的自定义
属性
@Html.DropDownListFor(m => m.FavoriteColor, (IEnumerable<SelectListItem>)ViewBag.Colors, new {id="username" })
@Html.DropDownListFor(m=>m.FavoriteColor,(IEnumerable)ViewBag.Colors,新的{id=“username”})
很明显,我将向helper方法添加另一个参数,但是,我将对EnumEditorHtmlHelper类进行必要的更改。这是您的自定义类吗?