用于扩展描述和短名称字段的枚举下拉列表
Enumdropdownlistfor extending for description and shortname fields
MVC 的 EnumDropDownListFor html 助手不呈现 Description 和 ShortName 属性。我需要为呈现的选项标签自定义属性文本。我搜索了很多而不是重写关于 MVC 的所有内容,但我找不到任何东西。
我知道 MVC 与 WebForms 有很大不同,但是,MVC 应该提供一种自定义渲染机制的方法。
根据我的搜索,我首先需要读取 Enum 类型的所有成员,然后重写包含验证的渲染机制。修改基本方法的 html 最糟糕的选择是使用正则表达式。
结果代码如下所示:
public static MvcHtmlString EnumDropDownListForEx<T, TProperty>(this HtmlHelper<T> htmlHelper, Expression<Func<T, TProperty>> expression,
object htmlAttributes, string placeholder = "")
{
var type = Nullable.GetUnderlyingType(typeof(TProperty)) ?? typeof(TProperty);
var values = Enum.GetValues(type);
var name = ExpressionHelper.GetExpressionText(expression);
var fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
var select = new TagBuilder("select");
select.MergeAttribute("name", fullHtmlFieldName);
select.MergeAttributes(new RouteValueDictionary(htmlAttributes));
var option = new TagBuilder("option");
option.MergeAttribute("value", "");
option.MergeAttribute("selected", "selected");
option.InnerHtml = placeholder;
var sb = new StringBuilder();
sb.Append(option.ToString(TagRenderMode.Normal));
foreach (Enum value in values)
{
option = new TagBuilder("option");
option.MergeAttribute("value", value.ToInt().ToString());
option.InnerHtml = value.GetEnumDescription();
var attr = value.GetAttribute<DisplayAttribute>();
if(attr == null)
continue;
option.InnerHtml = attr.Name;
option.MergeAttribute("description", attr.Description);
option.MergeAttribute("shortname", attr.ShortName);
sb.Append(option.ToString(TagRenderMode.Normal));
}
select.InnerHtml = sb.ToString();
select.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name));
return MvcHtmlString.Create(select.ToString(TagRenderMode.Normal));
}
MVC 的 EnumDropDownListFor html 助手不呈现 Description 和 ShortName 属性。我需要为呈现的选项标签自定义属性文本。我搜索了很多而不是重写关于 MVC 的所有内容,但我找不到任何东西。
我知道 MVC 与 WebForms 有很大不同,但是,MVC 应该提供一种自定义渲染机制的方法。
根据我的搜索,我首先需要读取 Enum 类型的所有成员,然后重写包含验证的渲染机制。修改基本方法的 html 最糟糕的选择是使用正则表达式。 结果代码如下所示:
public static MvcHtmlString EnumDropDownListForEx<T, TProperty>(this HtmlHelper<T> htmlHelper, Expression<Func<T, TProperty>> expression,
object htmlAttributes, string placeholder = "")
{
var type = Nullable.GetUnderlyingType(typeof(TProperty)) ?? typeof(TProperty);
var values = Enum.GetValues(type);
var name = ExpressionHelper.GetExpressionText(expression);
var fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
var select = new TagBuilder("select");
select.MergeAttribute("name", fullHtmlFieldName);
select.MergeAttributes(new RouteValueDictionary(htmlAttributes));
var option = new TagBuilder("option");
option.MergeAttribute("value", "");
option.MergeAttribute("selected", "selected");
option.InnerHtml = placeholder;
var sb = new StringBuilder();
sb.Append(option.ToString(TagRenderMode.Normal));
foreach (Enum value in values)
{
option = new TagBuilder("option");
option.MergeAttribute("value", value.ToInt().ToString());
option.InnerHtml = value.GetEnumDescription();
var attr = value.GetAttribute<DisplayAttribute>();
if(attr == null)
continue;
option.InnerHtml = attr.Name;
option.MergeAttribute("description", attr.Description);
option.MergeAttribute("shortname", attr.ShortName);
sb.Append(option.ToString(TagRenderMode.Normal));
}
select.InnerHtml = sb.ToString();
select.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name));
return MvcHtmlString.Create(select.ToString(TagRenderMode.Normal));
}