我使用MVC5 DataType.Date:
创建了一个日期选择器[DataType(DataType.Date)]
public DateTime ArrivalDate { get; set; }
如何将最短可用日期设置为今天并禁用所有过去的日期?
或者,是否可以选择禁用自定义日期范围?
我尝试使用this answer创建自定义DateRangeValidator,但是我得到了以下错误:named parameter type constraints
这是我的日期范围验证器:
public DateTime FirstDate = DateTime.Today.Date;
//public DateTime SecondDate { get; set; }
protected override ValidationResult IsValid(DateTime date, ValidationContext validationContext)
{
// your validation logic
if (date >= DbFunctions.TruncateTime(FirstDate))
{
return ValidationResult.Success;
}
else
{
return new ValidationResult("Date is not valid.");
}
}
答案 0 :(得分:1)
尝试使用您自己的逻辑构建自定义验证属性:
public sealed class PresentOrFutureDateAttribute: ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// your validation logic
if (Convert.ToDateTime(value) >= DateTime.Today)
{
return ValidationResult.Success;
}
else
{
return new ValidationResult("Past date not allowed.");
}
}
}