Frage

I am trying to implement StringLengthAttribute class in order to overwrite FormatErrorMessage() method.

However, after adding the new StringLengthWithCustomErrorMessage annotation to the property, the unobtrusive validation is missing from the html output:

data-val-length="Error message"

I am missing something?

public class StringLengthWithCustomErrorMessageAttribute : StringLengthAttribute
{
    public StringLengthWithCustomErrorMessageAttribute(int maximumLength)
        :base(maximumLength)
    {
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format("Value must be between {0} and {1} characters", MinimumLength, MaximumLength);
    }
}
War es hilfreich?

Lösung

You need to add an adapter for your custom attribute StringLengthWithCustomErrorMessageAttribute

protected void Application_Start() {
  DataAnnotationsModelValidatorProvider
    .RegisterAdapter(typeof(StringLengthWithCustomErrorMessageAttribute), 
    typeof(StringLengthAttributeAdapter));
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top