[Display(ResourceType = typeof(MyLocalization.CommonModelsStrings), Name = "User_LastName")]
public string LastName { get; set; }
Elsewhere you can access the localized display string of the model class without instantiating it using the following method. I put it in a static helper class named ModelAnnotationsUtils for easy access:
public static string GetLocalizedDisplay<TModel>(string pPropertyName) { DisplayAttribute attribute = ((DisplayAttribute)(typeof(TModel).GetProperty(pPropertyName).GetCustomAttributes(typeof(DisplayAttribute), true)[0])); ResourceManager rm = new ResourceManager(attribute.ResourceType); return rm.GetString(attribute.Name); }
Usage:
string lastName = ModelAnnotationsUtils.GetLocalizedDisplay<Model.User>("LastName");
No comments:
Post a Comment