2

There are multiple data annotation attributes in C#, such as [Required], [MaxLength] etc, each of which has it own default validation message.

What I trying to achieve here is to override the default validation message without inheriting the attribute in following way. I need a base for this, and not simply put [MaxLength(5, Error Message = "Custom Error Message")]. Put in in simple term, when i decorate with [MaxLength(5)], the output of model validation error should be "Custom Error Message" instead of default error message. What I have achieve now is need to use [MyMaxLength(5)] instead.

public class MyMaxLengthAttribute : MaxLengthAttribute
{
    public MyMaxLengthAttribute(int length) : base(length)
    {
        ErrorMessage = "Custom Error Message"
    }
}
stackdisplay
  • 1,949
  • 6
  • 29
  • 46
  • 1
    One option is to create a resource file for default error messages as per [this answer](http://stackoverflow.com/questions/6214066/how-to-change-default-validation-error-message-in-asp-net-mvc) –  Oct 30 '15 at 00:31
  • Another option is to specify your error message in a custom model binder as described [in this answer](http://stackoverflow.com/a/4043070/1474386) – holdenmcgrohen Oct 30 '15 at 12:59

0 Answers0