I have fields that different people should see in different names.
For example, suppose I have the following user types:
public enum UserType {Expert, Normal, Guest}
I applied the IMetadataAware attribute:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] public class DisplayForUserTypeAttribute : Attribute, IMetadataAware { private readonly UserType _userType; public DisplayForUserTypeAttribute(UserType userType) { _userType = userType; } public string Name { get; set; } public void OnMetadataCreated(ModelMetadata metadata) { if (CurrentContext.UserType != _userType) return; metadata.DisplayName = Name; } }
The idea is that I can override other values ββas needed, but I will not return to the default values ββwhen I do not. For example:
public class Model { [Display(Name = "Age")] [DisplayForUserType(UserType.Guest, Name = "Age (in years, round down)")] public string Age { get; set; } [Display(Name = "Address")] [DisplayForUserType(UserType.Expert, Name = "ADR")] [DisplayForUserType(UserType.Normal, Name = "The Address")] [DisplayForUserType(UserType.Guest, Name = "This is an Address")] public string Address { get; set; } }
The problem is that when I have several attributes of the same type, DataAnnotationsModelMetadataProvider only works OnMetadataCreated for the first. In the above example, Address can only be displayed as "Address" or "ADR" - other attributes are never executed.
If I try to use different attributes - DisplayForUserType , DisplayForUserType2 , DisplayForUserType3 , everything works as expected.
Am I doing something wrong here?