Datatype.Date 如何将最短日期设置为今天?

Datatype.Date how to set minimum date to Today?

我使用 MVC5 创建了一个日期选择器 DataType.Date:

[DataType(DataType.Date)]
public DateTime ArrivalDate { get; set; }

如何将最短可用日期设置为今天并禁用所有过去的日期?

或者,是否有禁用自定义日期范围的选项?

我尝试使用 this answer, but with that I get the following error: named parameter type constraints

创建自定义 DateRangeValidator

这是我的日期范围验证器:

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.");
        }
    }

尝试使用您自己的逻辑构建自定义验证属性:

        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.");
                }
            }
        }