Asp.net mvc 实现StringLengthAttribute类不起作用

Asp.net mvc 实现StringLengthAttribute类不起作用,asp.net-mvc,data-annotations,Asp.net Mvc,Data Annotations,我试图实现StringLengthAttribute类,以便覆盖FormatErrorMessage()方法 但是,在向属性添加新的StringLengthWithCustomErrorMessage注释后,html输出中缺少不引人注目的验证: data val length=“错误消息” 我错过了什么 public class StringLengthWithCustomErrorMessageAttribute : StringLengthAttribute { public Str

我试图实现
StringLengthAttribute
类,以便覆盖
FormatErrorMessage()
方法

但是,在向属性添加新的
StringLengthWithCustomErrorMessage
注释后,html输出中缺少不引人注目的验证:

data val length=“错误消息”

我错过了什么

public class StringLengthWithCustomErrorMessageAttribute : StringLengthAttribute
{
    public StringLengthWithCustomErrorMessageAttribute(int maximumLength)
        :base(maximumLength)
    {
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format("Value must be between {0} and {1} characters", MinimumLength, MaximumLength);
    }
}

您需要为自定义属性StringLengthWithCustomErrorMessageAttribute添加适配器

protected void Application_Start() {
  DataAnnotationsModelValidatorProvider
    .RegisterAdapter(typeof(StringLengthWithCustomErrorMessageAttribute), 
    typeof(StringLengthAttributeAdapter));
}

为什么不将错误消息传递给
StringLengthAttribute
[StringLength(10,MinimumLength=1,ErrorMessage=“Value必须介于{2}和{1}个字符之间”)
?这很有效,谢谢。然而,我仍然很高兴知道为什么我的尝试没有成功