自定义 DataAnnotation 属性

发布于 2024-10-16 16:01:12 字数 913 浏览 6 评论 0原文

当我在 ASP.NET MVC 3 模型中使用 DisplayAttribute 时,编写它们很快就会变得很痛苦,因为我们必须对字符串进行硬编码,或者从某个包含 const strings 的静态类中引用该字符串(这就是我现在已经有了,见下文)。但即使这样对我来说也太过分了。

我想提出一个名为 [SimpleDisplay] 的属性,它会通过查看

  1. 类名、
  2. 属性所附加的

属性名来隐式构造资源字符串。这可能吗?

像这样的东西

public class Product {

 [SimpleDisplay] // it will take Product and Name and do something like this Product_Name
 public string Name { get; set; } 

}

如果可能的话,这就是我想要摆脱的东西:

    [Display(ResourceType = typeof(Resources.Localize), Name = ResourceStrings.product_prettyid)]
    public virtual int PrettyId
    {
        get;
        set;
    }

    [Display(ResourceType = typeof(Resources.Localize), Name = ResourceStrings.product_name)]
    public virtual string Title
    {
        get;
        set;
    }

现在我知道不可能继承 DisplayAttribute 因为它是密封的。我还有什么其他选择?这还有道理吗?

When I use DisplayAttribute in ASP.NET MVC 3 models it quickly becomes a pain writing them because we have to either hardcode the string or reference the string from a some static class that contains const strings (which is what I have now, see below). But even that is too much for me.

I would like to come up with an attribute that would be called something like [SimpleDisplay] and it would implicitly construct the string for resources by looking at

  1. class name,
  2. property name that the attribute is attached to.

Is this possible?

Something like this

public class Product {

 [SimpleDisplay] // it will take Product and Name and do something like this Product_Name
 public string Name { get; set; } 

}

This is what I want to get rid of, if possible:

    [Display(ResourceType = typeof(Resources.Localize), Name = ResourceStrings.product_prettyid)]
    public virtual int PrettyId
    {
        get;
        set;
    }

    [Display(ResourceType = typeof(Resources.Localize), Name = ResourceStrings.product_name)]
    public virtual string Title
    {
        get;
        set;
    }

Now I know that it is not possible to inherit the DisplayAttribute cause it's sealed. What other options I have? Does it even make sense?

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

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

发布评论

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

评论(2

笑着哭最痛 2024-10-23 16:01:12

我会尝试仅创建一个标准属性和自定义 DataAnnotationsModelMetadataProvider。您可以重写 CreateMetadata 方法,该方法获取 IEnumerable。您应该搜索您的属性

attributes.OfType<SimpleDisplayAttribute>().FirstOrDefault();

并以您想要的任何方式填充模型元数据。

I would try creating just a standard attribute and custom DataAnnotationsModelMetadataProvider. You can override CreateMetadata method, which gets IEnumerable<Attribute>. You should than search for your attribute

attributes.OfType<SimpleDisplayAttribute>().FirstOrDefault();

and populate model metadata in any way you want.

帅哥哥的热头脑 2024-10-23 16:01:12

如果我正确理解您的意思,您可以创建一个简单的自定义属性,如下所示:

public class LocalizedDisplayNameAttribute : DisplayNameAttribute {
    public LocalizedDisplayNameAttribute(string expression) : base(expression) { }

    public override string DisplayName {
        get {
            try {
                string[] vals = base.DisplayName.Split(',');
                if(vals != null && vals.Length == 2)
                    return (string)HttpContext.GetGlobalResourceObject(vals[0].Trim(), vals[1].Trim());
            } catch {}
            return "{res:" + base.DisplayName + "}";
        }
    }
}

然后您可以将其用作您的属性的属性。 MVC HTML 扩展将获取您的自定义属性。

[LocalizedDisplayName("LBL, lbl_name1")]
public string[] Name1 { get; set; }

If i have a correct understanding what you mean, you may just create a simple custom attribute like this one:

public class LocalizedDisplayNameAttribute : DisplayNameAttribute {
    public LocalizedDisplayNameAttribute(string expression) : base(expression) { }

    public override string DisplayName {
        get {
            try {
                string[] vals = base.DisplayName.Split(',');
                if(vals != null && vals.Length == 2)
                    return (string)HttpContext.GetGlobalResourceObject(vals[0].Trim(), vals[1].Trim());
            } catch {}
            return "{res:" + base.DisplayName + "}";
        }
    }
}

You may then use it as an attribute on your properies. MVC HTML extensions will pickup your custom attribute.

[LocalizedDisplayName("LBL, lbl_name1")]
public string[] Name1 { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文