如何从自定义属性获取属性的显示名称

发布于 2024-09-30 23:37:46 字数 794 浏览 6 评论 0原文

我正在尝试创建一个最小长度验证属性,该属性将强制用户在文本框中输入指定的最小数量的字符

    public sealed class MinimumLengthAttribute : ValidationAttribute
        {
            public int MinLength { get; set; }

            public MinimumLengthAttribute(int minLength)
            {
                MinLength = minLength;
            }

            public override bool IsValid(object value)
            {
                if (value == null)
                {
                    return true;
                }
                string valueAsString = value as string;
                return (valueAsString != null && valueAsString.Length >= MinLength);

  }
    }

在 MaximumLengthAttribute 的构造函数中,我想将错误消息设置如下:

ErrorMessage =“{0} 必须是至少 {1} 个字符长”

如何获取属性的显示名称以便填充 {0} 占位符?

I am trying to create a minimum length validation attribute which will force users to enter the specified minimum amount of characters into a textbox

    public sealed class MinimumLengthAttribute : ValidationAttribute
        {
            public int MinLength { get; set; }

            public MinimumLengthAttribute(int minLength)
            {
                MinLength = minLength;
            }

            public override bool IsValid(object value)
            {
                if (value == null)
                {
                    return true;
                }
                string valueAsString = value as string;
                return (valueAsString != null && valueAsString.Length >= MinLength);

  }
    }

In the constructor of the MinimumLengthAttribute I would like to set the error message as follows:

ErrorMessage = "{0} must be atleast {1} characters long"

How can I get the property's display name so that I can populate the {0} placeholder?

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

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

发布评论

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

评论(3

玩套路吗 2024-10-07 23:37:47

您可以覆盖

protected override ValidationResult IsValid(object value, ValidationContext validationContext)

并使用 validationContext.DisplayName

You can override

protected override ValidationResult IsValid(object value, ValidationContext validationContext)

and use validationContext.DisplayName

一向肩并 2024-10-07 23:37:46

{0} 占位符会自动填充 [Display(Name="")] 的值,并且如果 [Display(Name= "")] 属性不存在,那么它将采用属性的名称

The {0} placeholder is automatically populated with the value for [Display(Name="<value>")] and if the [Display(Name="")] attribute doesn't exist then It will take the Name of the property.

随心而道 2024-10-07 23:37:46

如果您的错误消息有多个占位符,则您的属性还应覆盖 FormatErrorMessage 方法如下:

public override string FormatErrorMessage(string name) {
    return String.Format(ErrorMessageString, name, MinLength);
}

您应该调用构造函数重载之一来指定属性的默认错误消息:

public MinimumLengthAttribute()
    : base("{0} must be at least {1} characters long") {
}

If your error message has more than one placeholder, they your attribute should also override the FormatErrorMessage method like so:

public override string FormatErrorMessage(string name) {
    return String.Format(ErrorMessageString, name, MinLength);
}

And you should call one of the constructor overloads to specfiy your attribute's default error message:

public MinimumLengthAttribute()
    : base("{0} must be at least {1} characters long") {
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文