Substitution arguments in ValidationAttribute.ErrorMessage

后端 未结 1 1392
醉梦人生
醉梦人生 2021-01-04 08:50

In an ASP.NET MVC 4 app, the LocalPasswordModel class (in Models\\AccountModels.cs) looks like this:

public class LocalPasswordModel
{
    [Required]
    [Da         


        
相关标签:
1条回答
  • 2021-01-04 09:00

    For StringLengthAttribute, the message string can take 3 arguments:

    {0} Property name
    {1} Maximum length
    {2} Minimum length
    

    These parameters unfortunately do not seem to be well documented. The values are passed in from each validation attribute's FormatErrorMessage attribute. For example, using .NET Reflector, here is that method from StringLengthAttribute:

    public override string FormatErrorMessage(string name)
    {
        EnsureLegalLengths();
        string format = ((this.MinimumLength != 0) && !base.CustomErrorMessageSet) ? DataAnnotationsResources.StringLengthAttribute_ValidationErrorIncludingMinimum : base.ErrorMessageString;
        return String.Format(CultureInfo.CurrentCulture, format, new object[] { name, MaximumLength, MinimumLength });
    }
    

    It is safe to assume that this will never change because that would break just about every app that uses it.

    0 讨论(0)
提交回复
热议问题