【问题标题】:Displaying friendly, localized enum values using DataAnnotations in ASP.NET MVC2在 ASP.NET MVC2 中使用 DataAnnotations 显示友好的本地化枚举值
【发布时间】:2010-08-19 12:14:23
【问题描述】:

在 MVC2 中显示本地化枚举属性的推荐方法是什么?

如果我有这样的模型:

public class MyModel {
  public MyEnum MyEnumValue { get; set; } 
}

视图中的一行是这样的:

<%: Html.DisplayFor(model => model.MyEnumValue) %>

我希望像这样用DisplayAttribute 注释枚举值:

public enum MyEnum
{
    [Display(Name="EnumValue1_Name", ResourceType=typeof(Resources.MyEnumResources))]
    EnumValue1,
    [Display(Name="EnumValue2_Name", ResourceType=typeof(Resources.MyEnumResources))]
    EnumValue2,
    [Display(Name="EnumValue3_Name", ResourceType=typeof(Resources.MyEnumResources))]
    EnumValue3
}

不支持。看来还需要点别的东西。最好的实现方式是什么?

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-2


【解决方案1】:

您可以尝试为此使用 DescriptionAttribute。

例如

在视图模型中:

public enum MyEnum
        {
             [Description("User Is Sleeping")]
            Asleep,
             [Description("User Is Awake")]
            Awake
        }

 public string GetDescription(Type type, string value)
        {
            return ((DescriptionAttribute)(type.GetMember(value)[0].GetCustomAttributes(typeof(DescriptionAttribute), false)[0])).Description;
        }

在视图中:

Model.GetDescription(Model.myEnum.GetType(), Model.myEnum) to retrieve the value set in Description. 

我正在使用类似的东西在我的 Html.Dropdown 中设置显示名称和值。

【讨论】:

  • 当然,但这不是本地化的。视图需要知道它们是在显示 Enum 还是其他东西,我希望将这个问题分开。我希望扩展 ModelMetadataProvider 或类似的东西。如果我找到它,我会发布答案。
  • 是的,你是对的。这不适用于本地化请求。不过我会保留这个职位。有些人可能会发现它对其他场景很有用。
【解决方案2】:

我也使用 Display 注释。这是我最终使用的,它适用于属性和枚举成员。

这是我的枚举:

public enum TagOrderStatus
{
    [Display(ResourceType = typeof(TagStrings), Name = "TagOrderStatus_NotOrdered")]
    NotOrdered = 0,
    [Display(ResourceType = typeof(TagStrings), Name = "TagOrderStatus_ToOrder")]   
    ToOrder = 1,
    [Display(ResourceType = typeof(TagStrings), Name = "TagOrderStatus_Ordered")]   
    Ordered = 2
}

然后是我的小全能实用方法:

public static string GetLocalizedDisplay<TModel>(string pPropertyName)
{
    DisplayAttribute attribute;

    if (typeof(TModel).IsEnum)
    {
        MemberInfo member = typeof(TModel).GetMembers().SingleOrDefault(m => m.MemberType == MemberTypes.Field && m.Name == pPropertyName);
        attribute = (DisplayAttribute)member.GetCustomAttributes(typeof(DisplayAttribute), false)[0];
    }
    else
    {
        PropertyInfo property = typeof(TModel).GetProperty(pPropertyName);
        attribute = (DisplayAttribute)property.GetCustomAttributes(typeof(DisplayAttribute), true)[0];
    }

    if (attribute.ResourceType != null)
        return new ResourceManager(attribute.ResourceType).GetString(attribute.Name);
    else
        return attribute.Name;
}

然后可以使用这种方式为枚举成员获取单个成员显示属性:

string disp = GetLocalizedDisplay<Tag.TagOrderStatus>("Ordered");

或属性:

string disp = GetLocalizedDisplay<Tag>("OwnerName");

我喜欢泛型。希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多