如何将 DisplayName 置于 ErrorMessage 格式中

发布于 2024-09-15 10:28:18 字数 768 浏览 8 评论 0原文

我有这样的内容:

    [DisplayName("First Name")]
    [Required(ErrorMessage="{0} is required.")]
    [StringLength(50, MinimumLength = 10, ErrorMessage="{0}'s length should be between {2} and {1}.")]
    public string Name { get; set; }

我想要以下输出:

  • 需要名字。
  • 名字的长度应该在 10 到 50 之间。

使用 ASP.NET MVC2 错误摘要时它可以工作,但是当我尝试手动验证它时,如下所示:

        ValidationContext context = new ValidationContext(myModel, null, null);
        List<ValidationResult> results = new List<ValidationResult>();
        bool valid = Validator.TryValidateObject(myModel, context, results, true);

结果是:

  • 名称是必需的。
  • 名称长度应在 10 到 50 之间。

有什么问题吗?谢谢。

I have something like this:

    [DisplayName("First Name")]
    [Required(ErrorMessage="{0} is required.")]
    [StringLength(50, MinimumLength = 10, ErrorMessage="{0}'s length should be between {2} and {1}.")]
    public string Name { get; set; }

I want to have the following output:

  • First Name is required.
  • First Name's length should be between 10 and 50.

It is working when using ASP.NET MVC2 Error Summary, but when I try to validate it manually, like this:

        ValidationContext context = new ValidationContext(myModel, null, null);
        List<ValidationResult> results = new List<ValidationResult>();
        bool valid = Validator.TryValidateObject(myModel, context, results, true);

The results are:

  • Name is required.
  • Name's length should be between 10 and 50.

What's wrong? Thanks.

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

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

发布评论

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

评论(2

梦过后 2024-09-22 10:28:23

不要使用(或可能结合使用)[DisplayName] 属性,而是使用 System.ComponentModel.DataAnnotations 中的 [Display] 属性。填充其 Name 属性。

这样,您就可以将内置验证属性或自定义属性与 ValidationContextDisplayName 结合使用。

例如,

[Display(Name="First Name")] // <-- Here
[Required(ErrorMessage="{0} is required.")]
[StringLength(50, MinimumLength = 10, ErrorMessage="{0}'s length should be between {2} and {1}.")]
public string Name { get; set; }

Instead of (or perhaps in conjunction with) using the [DisplayName] attribute, use the [Display] attribute in System.ComponentModel.DataAnnotations. Populate its Name property.

With that, you can use built-in validation attributes or custom attributes with ValidationContext's DisplayName.

e.g.,

[Display(Name="First Name")] // <-- Here
[Required(ErrorMessage="{0} is required.")]
[StringLength(50, MinimumLength = 10, ErrorMessage="{0}'s length should be between {2} and {1}.")]
public string Name { get; set; }
沉溺在你眼里的海 2024-09-22 10:28:23

嗯,我想我做到了。

我必须创建另一个像这样的属性:

public class RequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
    private String displayName;

    public RequiredAttribute()
    {
        this.ErrorMessage = "{0} is required";
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var attributes = validationContext.ObjectType.GetProperty(validationContext.MemberName).GetCustomAttributes(typeof(DisplayNameAttribute), true);
        if (attributes != null)
            this.displayName = (attributes[0] as DisplayNameAttribute).DisplayName;
        else
            this.displayName = validationContext.DisplayName;

        return base.IsValid(value, validationContext);
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(this.ErrorMessageString, displayName);
    } 
}

我的模型是:

    [DisplayName("Full name")]
    [Required]
    public string Name { get; set; }

值得庆幸的是,这个 DataAnnotation 是可扩展的。

Well, I think I did it.

I had to create another attribute like this:

public class RequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
    private String displayName;

    public RequiredAttribute()
    {
        this.ErrorMessage = "{0} is required";
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var attributes = validationContext.ObjectType.GetProperty(validationContext.MemberName).GetCustomAttributes(typeof(DisplayNameAttribute), true);
        if (attributes != null)
            this.displayName = (attributes[0] as DisplayNameAttribute).DisplayName;
        else
            this.displayName = validationContext.DisplayName;

        return base.IsValid(value, validationContext);
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(this.ErrorMessageString, displayName);
    } 
}

And my model is:

    [DisplayName("Full name")]
    [Required]
    public string Name { get; set; }

Thankfully this DataAnnotation is extensible.

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