Вот рабочий пример:
public class PermittedYearRangeAttribute : RangeAttribute
{
public PermittedYearRangeAttribute()
: base(1900, DateTime.Now.AddYears(-50).Year)
{
ErrorMessage = string.Format("Year must be between 1900 and {0}", DateTime.Now.AddYears(-50).Year);
}
}
В Global.asax.cs
Application_Start()
метод добавить следующую строку
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(PermittedYearRangeAttribute), typeof(RangeAttributeAdapter));
В свойстве Model Decorate:
[Required(ErrorMessage = "Please enter year")]
[PermittedYearRange]
public int Year { get; set; }
HTML предоставлено:
<input class="tooltip form-control input dob--input input-validation-error" data-val="true" data-val-number="The field Year must be a number." data-val-range="Year must be between 1900 and 1965" data-val-range-max="1965" data-val-range-min="1900" data-val-required="Please enter year" id="Year" maxlength="4096" name="Year" placeholder="YYYY" tabindex="" type="text" value="0">