1

I have enum property

public enum MyType
{
    House = 0, 
    Apartment = 1
}

now I want to localize this enum using localized resx strings, so I add

public enum MyType
{
    [LocalizedName("House", typeof(LocalizedUIStrings))]
     House = 0, 
    [LocalizedName("Apartment ", typeof(LocalizedUIStrings))]
     Apartment = 1
 }

and localizedname helper class

public class LocalizedNameAttribute : Attribute
{
    private readonly Type _resourceType;
    private readonly string _resourceKey;

    public LocalizedNameAttribute(string resourceKey, Type resourceType)
    {
       _resourceType = resourceType;
       _resourceKey = resourceKey;
       DisplayName = (string)_resourceType
           .GetProperty(_resourceKey, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
          .GetValue(null, null);
    }
    public string DisplayName { get; private set; }
}

in the view I'm trying to use this localized property like this

@Html.DisplayFor(modelItem => item.MyType)

It still returns non localized string, no errors (either on compile or runtime). What I'm doing wrong here?

user1765862
  • 13,635
  • 28
  • 115
  • 220
  • 1
    The `DisplayFor()` method does not know anything about your `LocalizedNameAttribute` It renders it output based on the `ModelMetadata` of the property. You will need to create you own html helper. –  May 10 '15 at 09:39
  • thank you. Any suggestion how to do this? – BobRock May 10 '15 at 10:10
  • You can check this => http://stackoverflow.com/questions/17380900/enum-localization – CodeNotFound May 10 '15 at 13:24

0 Answers0