如何通过 ASP.NET MVC2 使用和/或本地化 DisplayAttribute?

发布于 2024-08-26 12:26:16 字数 741 浏览 6 评论 0原文

可能的重复:
资源中的 DisplayName 属性?

我试图弄清楚如何在 MVC 2 中获取 DisplayAttribute ViewModel 与 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。有人运行过吗?

Possible Duplicate:
DisplayName attribute from Resources?

I am trying to figure out how to get the DisplayAttribute in my MVC 2 ViewModel to work with the Html.LabelFor() helper.

Neither

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

nor

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

seem to work. Localizing the Required Attribute works as expected:

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

I am using VS2010 RC. Has anybody got that running?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烧了回忆取暖 2024-09-02 12:26:16

[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,
我们将在哪里进行编译
数据注释 v4。

您有一些解决方法。当.NET
4个RTM,我们将提供.NET
4 特定的 Futures 二进制文件,以及
期货二进制文件将有一个元数据
理解[显示]的提供商
和其他特定于 DataAnnotations v4 的
属性。或者,如果您需要
立即找到解决方案,将其子类化
[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(); }
  }
}

The [Display] attribute is a .NET 4-specific attribute. Since MVC 2 is compiled against .NET 3.5, the runtime does not recognize this attribute.

See http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId=5515 for more information plus workarounds.

Edit:

Eh, the work item's not that big. May as well include it inline. :)

The [Display] attribute is new in
DataAnnotations v4, so MVC 2 can't use
it because we're compiled against
DataAnnotations v3.5. Use
[DisplayName] instead until MVC 3,
where we will be compiled against
DataAnnotations v4.

You have a few workarounds. When .NET
4 RTMs, we will provide a .NET
4-specific Futures binary, and that
Futures binary will have a metadata
provider that understands [Display]
and other DataAnnotations v4-specific
attributes. Alternatively, if you need
a solution right away, subclass the
[DisplayName] attribute, make a
private DisplayNameAttribute field
that's instantiated appropriately, and
override the virtual
DisplayNameAttribute.DisplayName
property so that it delegates to
_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(); }
  }
}
漫漫岁月 2024-09-02 12:26:16

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

If you download ASP.NET MVC 2 Futures assembly, then you can use the DisplayAttribute. You just need to add DataAnnotations4ModelMetadataProvider.RegisterProvider(); to your Global.asax.cs

左秋 2024-09-02 12:26:16

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

Levi pretty much answered your question, and for the record here is a working version for 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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文