Get model name in custom display name attribute

Here is my development requirement,

My label values ​​are stored in a database, and I still want to use data annotation in a declarative way to make my model more readable.

And here is my approach,

I decided to write my own DisplayNameAttribute, where the default value provided by my model will be overwritten by the value retrieved from the database.

Here is the property defined in the model,

    [CustomDisplay(Name: "First Name")]
    [CustomRequired(ErrorMessage: "{0} is required")]
    public String FirstName { get; set; }

Here is the attribute name attribute class of the display name,

public class CustomDisplayAttribute : DisplayNameAttribute
{
    private string _defaultName;
    private string _displayName;

    public CustomDisplayAttribute(string Name)
    {
        _defaultName = Name;
    }

    public override string DisplayName
    {
        get
        {
            if (String.IsNullOrEmpty(_displayName))
            {
                _displayName = DAO.RetrieveValue(**ModelName**, _defaultName);
            }
            return _displayName;
        }
    }
}

Now you can see in the above code, ModelName is what I need, but I don't have it !!

ModelMetadataProviders.Current , . , , .

enter image description here

, ,

private static string GetModelName()
{
    var modelName = String.Empty;
    FieldInfo info = typeof(CachedAssociatedMetadataProvider<CachedDataAnnotationsModelMetadata>)
                        .GetField("_typeIds", BindingFlags.NonPublic | BindingFlags.Static);
    var types = (ConcurrentDictionary<Type, string>)info.GetValue(null);
    modelName = types.FirstOrDefault().Key.Name;
    return modelName;
}

, ( ). , , !

enter image description here

+4
1

. / ..

, , Microsoft MVC, ModelMetadataProvider:

public class CustomDisplayAttribute : Attribute
{
    public CustomDisplayAttribute(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }
}

ModelMetadataProvider:

public class DatabaseModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
  public DatabaseModelMetadataProvider()
  {
  }

  protected override ModelMetadata CreateMetadata(
    IEnumerable<Attribute> attributes, 
    Type containerType, 
    Func<object> modelAccessor, 
    Type modelType, 
    string propertyName)
  {
    var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

    var displayAttribute = containerType == null
        ? null as CustomDisplayAttribute
        : containerType.GetProperty(propertyName)
           .GetCustomAttributes(false)
           .OfType<CustomDisplayAttribute>()
           .FirstOrDefault();
    if (displayAttribute != null)
    {
      var displayValue = DAO.RetrieveValue(containerType.ToString(), displayAttribute.Name)

      metadata.DisplayName = displayValue;
    }
    return metadata;
  }
}

public class MyViewModel
{
  public MyPropertyType PropertyName { get; set; }
}
  • containerType = MyViewModel
  • modelType = MyPropertyType
  • propertyName = PropertyName

(global.asax - ):

ModelMetadataProviders.Current = new LocalizedModelMetadataProvider();

ModelMetadata, , , , .

0

All Articles