覆盖 MVC 中的最大长度错误消息

sta*_*lay 3 c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我正在尝试覆盖 ASP.net MVC 中的最大长度错误消息。基本上,我想将错误消息字符串设置为如下:

[DisplayName] 长度不应超过 [x]。但是,我不知道如何在其中包含 displayname 属性值。

public class MyMaxLengthAttribute : MaxLengthAttribute
{
    public MyMaxLengthAttribute(int length) : base(length)
    {
        ErrorMessage = "What should I input here"
    }
}
Run Code Online (Sandbox Code Playgroud)

hut*_*oid 5

如果使用StringLengthAttribute {0}指的是显示名称,{2}指的是长度。

public class MyMaxLengthAttribute : StringLengthAttribute
{
    public MyMaxLengthAttribute(int length) : base(length)
    {
        ErrorMessage = "{0} length should not be more than {2}"
    }
}
Run Code Online (Sandbox Code Playgroud)