ASP .NET MVC 3 Data Annotations GreaterThan LowerThan for DateTime and int

后端 未结 5 2109
执念已碎
执念已碎 2021-02-13 10:19

I would like to know what is the easiest way to have a \"Greater Than\" & \"Lower Than\" validation on a ASP.NET MVC 3 form?

I use unobtrusive JavaScript for client

5条回答
  •  伪装坚强ぢ
    2021-02-13 10:29

    You can use the DateGreaterThanEqual attribute in your model. Here is a snippet of code that I used to validate two fields in my form.

    [DataType(DataType.Date)]
    [DisplayName("From Date")]
    public DateTime? StartDate { get; set; }
    
    [DataType(DataType.Date)]
    [DisplayName("To Date")]
    [DateGreaterThanEqual("StartDate")]
    public DateTime? EndDate { get; set; }
    

提交回复
热议问题