2010-08-19 78 views
18

在MVC2中顯示本地化枚舉屬性的推薦方式是什麼?在ASP.NET MVC2中使用DataAnnotations顯示友好的本地化枚舉值

如果我有這樣一個模型:

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 
} 

這不被支持。似乎還有其他需要的東西。實現它的最好方法是什麼?

+0

你想要顯示什麼作爲最終結果? – madcapnmckay 2010-08-19 12:23:32

+0

從資源文件中適當的翻譯,即EnumValue1_Name等 – 2010-08-19 12:46:44

+0

看看這個問題可能是有用的! http://stackoverflow.com/questions/3431515/in-asp-net-mvc-can-i-make-a-default-editor-template-for-enum – 2010-08-31 13:45:01

回答

7

您可以嘗試使用DescriptionAttribute。

E.g.

在視圖模型:

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的顯示名稱和價值的東西。

+1

當然,但這不是本地化。視圖需要知道他們是否顯示Enum或其他東西,我希望能分開這個問題。我希望擴展ModelMetadataProvider或類似的東西。如果我找到答案,我會發布答案。 – 2010-08-22 09:13:52

+1

是的,你是對的。這不適用於本地化請求。我會保留這個帖子。有些人可能會發現它適用於其他情況。 – 2010-08-23 04:09:09

+1

是的,我發現它很有用,謝謝。 – empo 2012-05-24 10:34:40

3

我也使用顯示註釋。以下是我最終使用的內容,這對於屬性和枚舉成員都適用。

這裏是我的枚舉:

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"); 

我喜歡泛型。希望這可以幫助!!