Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom display attribute using or inherits DisplayAttribute in ASP.NET MVC

Tags:

c#

asp.net-mvc

I want to use DisplayAttribute with Name property.

The problem is that class is sealed and I cannot inherits it to override some methods.

Why I want this ?

I want to pass some a code in order to translate strings to Name property. And add one property for language.

Something like:

[MyDisplay(Code = TRANSLATION_CODE, Language = "FR-FR")]
public string Fr { get; set; }

And inside MyDisplayAttribute, I want to do like:

public class MyDisplayAttribute: DisplayAttribute // it won't work the inherits
{
     public int Code { get; set; }
     public string Language { get; set; }

    // somewhere, I don't know what method
    // I want to assing `Name = GetTranslation(Code, Language);`
}

There is another way to do that ?


UPDATE

I tried also this:

public class MyDisplayAttribute : DisplayNameAttribute
   {
      private int _code;
      private string _language;

      public MyDisplayAttribute( int code, string language )
         : base( language )
      {
         _code = code;
         _language = language;
      }

      public override string DisplayName
      {
         get
         {
            // here come LanguageTranslatorManager
            if ( _code == 1 && _language == "en" ) {
               return "test";
            }

            return base.DisplayName;
         }
      }
   }

and in model:

  [MyDisplay( 1, "en" )]
  public string Test
  {
     get;
     set;
  }

I'm expecting to display test in view, but doesn't ! Where is my mistake ?

like image 730
Snake Eyes Avatar asked Dec 09 '13 07:12

Snake Eyes


People also ask

How can create custom attribute in core in asp net?

Declaring Custom AttributesWe can define an attribute by creating a class. This class should inherit from the Attribute class. Microsoft recommends appending the 'Attribute' suffix to the end of the class's name. After that, each property of our derived class will be a parameter of the desired data type.

How do you display attributes in C#?

For that we can use the Display attribute. DisplayAttribute or Display or DisplayName attribute: DisplayAttribute and Display is present in the System. ComponentModel. DataAnnotations namespace.

What is DisplayName attribute?

DisplayName. Gets the display name for a property, event, or public void method that takes no arguments stored in this attribute.


2 Answers

In order to set a specific display name for your property, you need to set the metadata property DisplayName. If you need to write custom attributes, you need to make sure that you create a custom metadata provider. Inside you need to set the DisplayName of your property, based on the values provided.

public class CustomModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes,
          Type containerType, Func<object> modelAccessor, 
          Type modelType, string propertyName)
    { 
         var modelMetadata = base.CreateMetadata(attributes, containerType, 
                 modelAccessor, modelType, propertyName);

         if (attributes.OfType<MyDisplay>().ToList().Count > 0)
         {
              modelMetadata.DisplayName = GetValueFromLocalizationAttribute(attributes.OfType<MyDisplay>().ToList()[0]);
         }

         return modelMetadata;
    }

    private string GetValueFromLocalizationAttribute(MyDisplay attribute)
    {
          return computedValueBasedOnCodeAndLanguage;
    }
}
like image 101
Andrei V Avatar answered Sep 28 '22 22:09

Andrei V


You are doing it wrong. The DisplayAttribute already supports 'translations' using the built-in .net internationalization

 [Display(Name = "property_name", ResourceType = typeof(MyResources))]

Only if that's not enough you should create your own attribute, but not deriving from Display.

Edit:

The DisplayAttribute is a sealed class, so there is no way to inherit from it.

like image 22
MikeSW Avatar answered Sep 28 '22 22:09

MikeSW