【问题标题】:How to use and/or localize DisplayAttribute with ASP.NET MVC2? [duplicate]如何通过 ASP.NET MVC2 使用和/或本地化 DisplayAttribute? [复制]
【发布时间】:2010-03-25 16:45:20
【问题描述】:

可能重复:
DisplayName attribute from Resources?

我试图弄清楚如何让 MVC 2 ViewModel 中的 DisplayAttribute 与 Html.LabelFor() 帮助器一起工作。

都没有

public class TestModel
{
    [Display(ResourceType = typeof(Localization.Labels))]
    public string Text { get; set; }
}

也没有

public class TestModel
{
    [Display(Name = "test")]
    public string Text { get; set; }
}

似乎有效。本地化所需属性按预期工作:

[Required(ErrorMessageResourceName = "Test", ErrorMessageResourceType = typeof(Localization.Labels))]

我正在使用 VS2010 RC。有人开过吗?

【问题讨论】:

    标签: asp.net asp.net-mvc-2 data-annotations


    【解决方案1】:

    [Display] 属性是特定于 .NET 4 的属性。由于 MVC 2 是针对 .NET 3.5 编译的,因此运行时无法识别此属性。

    有关更多信息和解决方法,请参阅http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId=5515

    编辑:

    呃,工作项没那么大。也可以将其包含在内。 :)

    [Display] 属性是新的 DataAnnotations v4,所以 MVC 2 不能使用 因为我们是针对 数据注释 v3.5。采用 [DisplayName] 直到 MVC 3, 我们将在哪里编译 DataAnnotations v4.

    您有一些解决方法。当.NET 4 个 RTM,我们将提供一个 .NET 4 特定的期货二进制文件,以及 期货二进制文件将有一个元数据 了解 [显示] 的提供商 和其他 DataAnnotations v4-specific 属性。或者,如果您需要 立即解决,子类化 [DisplayName] 属性,制作一个 私有 DisplayNameAttribute 字段 被适当地实例化,并且 覆盖虚拟 DisplayNameAttribute.DisplayName 属性,使其委托给 _theWrappedDisplayNameAttribute.GetName()。

    public class MultiCulturalDisplayName : DisplayNameAttribute {
      private DisplayAttribute display;
    
      public MultiCulturalDisplayName(Type resourceType, string resourceName) {
        this.display = new DisplayAttribute { ResourceType = resourceType, Name = resourceName };
      }
    
      public override string DisplayName {
        get { return display.GetName(); }
      }
    }
    

    【讨论】:

    • 是的,我现在使用了一个非常相似的解决方案。谢谢。
    【解决方案2】:

    如果您下载ASP.NET MVC 2 Futures 程序集,则可以使用 DisplayAttribute。您只需将DataAnnotations4ModelMetadataProvider.RegisterProvider(); 添加到您的 Global.asax.cs

    【讨论】:

      【解决方案3】:

      Levi 几乎回答了你的问题,这里记录的是 3.5 的工作版本

      [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
      public class DisplayNameLocalizedAttribute : DisplayNameAttribute
      {
          public DisplayNameLocalizedAttribute(Type resourceType, string resourceKey)
              : base(LookupResource(resourceType, resourceKey)) {  }
      
          internal static string LookupResource(Type resourceType, string resourceKey)
          {
              PropertyInfo property = resourceType.GetProperties().FirstOrDefault(p => p.PropertyType == typeof(System.Resources.ResourceManager));
              if (property != null)
              {
                  return ((ResourceManager)property.GetValue(null, null)).GetString(resourceKey);
              }
              return resourceKey;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-10-16
        • 2012-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 2011-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多