ASP.NET MVC:添加将 DisplayName 合并到自定义 ValidationAttribute 的自定义 ErrorMessage

发布于 2024-08-17 02:31:03 字数 912 浏览 6 评论 0原文

我正在使用带有 DataAnnotations 的 ASP.NET MVC。我创建了以下自定义 ValidationAttribute ,效果很好。

public class StringRangeAttribute : ValidationAttribute
{
    public int MinLength { get; set; }
    public int MaxLength { get; set; }

    public StringRangeAttribute(int minLength, int maxLength)
    {   
        this.MinLength = (minLength < 0) ? 0 : minLength;
        this.MaxLength = (maxLength < 0) ? 0 : maxLength;
    }

    public override bool IsValid(object value)
    {            
        //null or empty is <em>not</em> invalid
        string str = (string)value;
        if (string.IsNullOrEmpty(str))
            return true;

        return (str.Length >= this.MinLength && str.Length <= this.MaxLength);
    }
}

然而,出现的错误消息是标准的“字段 * 无效”。我想将其更改为:“[DisplayName] 必须在 [minlength] 和 [maxlength] 之间”,但是我无法弄清楚如何从此类中获取 DisplayName 甚至字段名称。

有人知道吗?

I am using ASP.NET MVC with DataAnnotations. I have created the following custom ValidationAttribute which works fine.

public class StringRangeAttribute : ValidationAttribute
{
    public int MinLength { get; set; }
    public int MaxLength { get; set; }

    public StringRangeAttribute(int minLength, int maxLength)
    {   
        this.MinLength = (minLength < 0) ? 0 : minLength;
        this.MaxLength = (maxLength < 0) ? 0 : maxLength;
    }

    public override bool IsValid(object value)
    {            
        //null or empty is <em>not</em> invalid
        string str = (string)value;
        if (string.IsNullOrEmpty(str))
            return true;

        return (str.Length >= this.MinLength && str.Length <= this.MaxLength);
    }
}

However, the error message that appears is the standard "The field * is invalid". I would like to change this to be: "The [DisplayName] must be between [minlength] and [maxlength]", however I cannot figure out how to get the DisplayName or even the name of the field from inside this class.

Anyone know?

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

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

发布评论

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

评论(1

空城仅有旧梦在 2024-08-24 02:31:03

稍微修改一下 StringLengthAttribute:

public class StringRangeAttribute : ValidationAttribute
{
    // Methods
    public StringRangeAttribute(int minimumLength, int maximumLength)
        : base(() => "The {0} must be between {1} and {2} chars long.")
    {
        MaximumLength = maximumLength;
        MinimumLength = minimumLength;
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, new object[] { name, MinimumLength ,MaximumLength });
    }

    public override bool IsValid(object value)
    {
        if (value != null)
        {
            return (((string)value).Length <= MaximumLength) && (((string)value).Length >= MinimumLength);
        }
        return true;
    }

    public int MaximumLength { get; set; }
    public int MinimumLength { get; set; }
}

slightly modified StringLengthAttribute:

public class StringRangeAttribute : ValidationAttribute
{
    // Methods
    public StringRangeAttribute(int minimumLength, int maximumLength)
        : base(() => "The {0} must be between {1} and {2} chars long.")
    {
        MaximumLength = maximumLength;
        MinimumLength = minimumLength;
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, new object[] { name, MinimumLength ,MaximumLength });
    }

    public override bool IsValid(object value)
    {
        if (value != null)
        {
            return (((string)value).Length <= MaximumLength) && (((string)value).Length >= MinimumLength);
        }
        return true;
    }

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