Mor*_*lus 5 reflection asp.net-mvc html-helper data-annotations asp.net-mvc-3
我[Display]在我的一个枚举中使用了该属性:
public enum eCommentType
{
[Display(ResourceType = typeof(FaultManagementStrings), Name = "NormalComment")]
NormalComment = 0,
[Display(ResourceType = typeof(FaultManagementStrings), Name = "OpenningComment")]
OpenningComment = 1,
[Display(ResourceType = typeof(FaultManagementStrings), Name = "StartProgressComment")]
StartProgressComment = 2,
[Display(ResourceType = typeof(FaultManagementStrings), Name = "ClouserComment")]
ClouserComment = 3,
[Display(ResourceType = typeof(FaultManagementStrings), Name = "ReopennignComment")]
ReopennignComment = 4
}
Run Code Online (Sandbox Code Playgroud)
是否可以创建一个Extention方法,该方法将重用从指定资源获取Display属性值的现有MVC finctionallity?
我会是那样的......
@Html.GetEnumDisplayAttributeValue(c=> comment.CommentType)
Run Code Online (Sandbox Code Playgroud)
我知道我可以用一些东西来实现所需的反射并找到资源类型的值和调用资源管理器等等......但我想也许有可能使用mvc功能内置的exsisting ..毕竟当你打电话给LabelFor帮手时已经完成了.
有可能或者我应该重新发明轮子?
我遇到了同样的问题并创建了这些扩展方法:
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
public static class EnumHelper
{
private static readonly SelectListItem[] SingleEmptyItem = new[] { new SelectListItem { Text = string.Empty, Value = string.Empty } };
public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
{
return htmlHelper.EnumDropDownListFor(expression, null);
}
public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, object htmlAttributes)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var enumType = GetNonNullableModelType(metadata);
var values = Enum.GetValues(enumType).Cast<TEnum>();
var items =
values.Select(value => new SelectListItem
{
Text = GetName(value),
Value = value.ToString(),
Selected = value.Equals(metadata.Model)
});
if (metadata.IsNullableValueType)
{
items = SingleEmptyItem.Concat(items);
}
return htmlHelper.DropDownListFor(expression, items, htmlAttributes);
}
private static string GetName<TEnum>(TEnum value)
{
var displayAttribute = GetDisplayAttribute(value);
return displayAttribute == null ? value.ToString() : displayAttribute.GetName();
}
private static DisplayAttribute GetDisplayAttribute<TEnum>(TEnum value)
{
return value.GetType()
.GetField(value.ToString())
.GetCustomAttributes(typeof(DisplayAttribute), false)
.Cast<DisplayAttribute>()
.FirstOrDefault();
}
private static Type GetNonNullableModelType(ModelMetadata modelMetadata)
{
var realModelType = modelMetadata.ModelType;
var underlyingType = Nullable.GetUnderlyingType(realModelType);
return underlyingType ?? realModelType;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在视图中使用这些扩展方法,如下所示:
@Html.EnumDropDownListFor(c=> comment.CommentType)
Run Code Online (Sandbox Code Playgroud)
您现在应该看到一个下拉列表,其中包含枚举值的名称DisplayAttribute.
显示的枚举值的实际检索在GetName方法中完成,该方法首先使用该GetDisplayAttribute方法来尝试检索DisplayAttribute枚举值.如果枚举值确实用a装饰DisplayAttribute,它将使用它来检索要显示的名称.如果没有DisplayAttribute,我们只返回枚举值的名称.
| 归档时间: |
|
| 查看次数: |
4001 次 |
| 最近记录: |