简体   繁体   English

MVC日期范围介于最小值和最大值之间

[英]MVC date range between min and max value

I am trying to add a range for date in my model. 我正在尝试在模型中添加日期范围。

I have added following property to my class 我在班级添加了以下属性

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Date Schedule")]
    [Range(typeof(DateTime), DateTime.Now.ToString("dd/MM/yyyy"), DateTime.Now.AddDays(120).ToString("dd/MM/yyyy"), ErrorMessage = "Please select valid date")]
    public DateTime? DateSchedule { get; set; }

It gives me following error 它给我以下错误

在此处输入图片说明

How can I assign Min and Max value to my DateSchedule ? 如何将最小值最大值分配给我的DateSchedule In calendar control it should only display date from today to 120 days (In my class I have added 120 days). 在日历控件中,它只能显示从today120 days (在我的课堂上,我已经添加了120天)。

Attributes accept only constants as parameters. 属性仅接受常量作为参数。

We know that DateTime.Now isn't a constant, it changes depending on when the code runs. 我们知道DateTime.Now不是常数,它会随着代码运行的时间而变化。 and Range attribute is determined at compile time. Range属性是在编译时确定的。

You need to create custom validator as shown below :- 您需要创建自定义验证器,如下所示:-

public class DateAttribute : RangeAttribute
{
    ////your code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM