在ASP.NET MVC中使用或继承DisplayAttribute创建自定义显示属性 [英] Create custom display attribute using or inherits DisplayAttribute in ASP.NET MVC

查看:110
本文介绍了在ASP.NET MVC中使用或继承DisplayAttribute创建自定义显示属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 DisplayAttribute Name 属性一起使用.

I want to use DisplayAttribute with Name property.

问题在于该类已被密封,并且我无法继承该类以覆盖某些方法.

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

我为什么要这个?

我想传递一些代码,以将字符串转换为 Name 属性.并为语言添加一个属性.

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

类似的东西:

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

在MyDisplayAttribute内,我想这样做:

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);`
}

还有另一种方法吗?

更新

我也尝试过这个:

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;
         }
      }
   }

和型号中:

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

我希望在视图中显示 test ,但不会!我的错误在哪里?

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

推荐答案

为设置属性的特定显示名称,您需要设置元数据属性 DisplayName .如果需要编写自定义属性,则需要确保创建自定义元数据提供程序.在内部,您需要根据提供的值设置属性的 DisplayName .

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;
    }
}

这篇关于在ASP.NET MVC中使用或继承DisplayAttribute创建自定义显示属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆