加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 从MVC中的资源获取[Display]属性值

发布时间:2020-12-16 06:49:04 所属栏目:asp.Net 来源:网络整理
导读:我使用了一个枚举的[Display]属性: public enum eCommentType { [Display(ResourceType = typeof(FaultManagementStrings),Name = "NormalComment")] NormalComment = 0,[Display(ResourceType = typeof(FaultManagementStrings),Name = "OpenningComment")
我使用了一个枚举的[Display]属性:

public enum eCommentType
    {
        [Display(ResourceType = typeof(FaultManagementStrings),Name = "NormalComment")]
        NormalComment = 0,[Display(ResourceType = typeof(FaultManagementStrings),Name = "OpenningComment")]
        OpenningComment = 1,Name = "StartProgressComment")]
        StartProgressComment = 2,Name = "ClouserComment")]
        ClouserComment = 3,Name = "ReopennignComment")]
        ReopennignComment = 4
    }

是否可以创建一个Extention方法,该方法将重用从指定资源获取Display属性值的现有MVC finctionallity?

我会是那样的……

@Html.GetEnumDisplayAttributeValue(c=> comment.CommentType)

我知道我可以用一些东西来实现所需的反射并找到资源类型的值和调用资源管理器等等……但我认为也许可以使用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>> 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;
    }
}

您可以在视图中使用这些扩展方法,如下所示:

@Html.EnumDropDownListFor(c=> comment.CommentType)

您现在应该看到一个下拉列表,其中包含根据DisplayAttribute的枚举值名称.

实际检索显示的枚举值是在GetName方法中完成的,该方法首先使用GetDisplayAttribute方法尝试检索枚举值的DisplayAttribute.如果枚举值确实用DisplayAttribute修饰,它将使用它来检索要显示的名称.如果没有DisplayAttribute,我们只返回枚举值的名称.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读