Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override max length error message in MVC

I am trying to override the max length error message in ASP.net MVC. Basically, I would like to make the error message string as follow:

[DisplayName] length should not be more than [x]. However, I do not know how to include the displayname attribute value inside.

public class MyMaxLengthAttribute : MaxLengthAttribute
{
    public MyMaxLengthAttribute(int length) : base(length)
    {
        ErrorMessage = "What should I input here"
    }
}
like image 714
stackdisplay Avatar asked Oct 18 '25 14:10

stackdisplay


1 Answers

If you use the StringLengthAttribute {0} refers to the display name and {2} refers to the length.

public class MyMaxLengthAttribute : StringLengthAttribute
{
    public MyMaxLengthAttribute(int length) : base(length)
    {
        ErrorMessage = "{0} length should not be more than {2}"
    }
}
like image 64
hutchonoid Avatar answered Oct 20 '25 04:10

hutchonoid