ASP.NET MVC: dodawanie niestandardowego komunikatu ErrorMessage, który zawiera DisplayName do niestandardowego ValidationAttribute
Korzystam z ASP.NET MVC z DataAnnotations. Stworzyłem następujący niestandardowy atrybut ValidationAttribute, który działa poprawnie.
public class StringRangeAttribute : ValidationAttribute
{
public int MinLength { get; set; }
public int MaxLength { get; set; }
public StringRangeAttribute(int minLength, int maxLength)
{
this.MinLength = (minLength < 0) ? 0 : minLength;
this.MaxLength = (maxLength < 0) ? 0 : maxLength;
}
public override bool IsValid(object value)
{
//null or empty is <em>not</em> invalid
string str = (string)value;
if (string.IsNullOrEmpty(str))
return true;
return (str.Length >= this.MinLength && str.Length <= this.MaxLength);
}
}
Jednak wyświetlany komunikat o błędzie jest standardem „Pole * jest nieprawidłowe”. Chciałbym zmienić to na: „[DisplayName] musi być pomiędzy [minlength] i [maxlength]”, jednak nie mogę dowiedzieć się, jak uzyskać DisplayName lub nawet nazwę pola z tej klasy.
Ktoś wie?