Greater Than or Equal To Today Date validation annotation in MVC3

前端 未结 3 1627
北恋
北恋 2020-12-05 02:53

Has anyone seen an MVC3 data annotation for Date validation that requires a single selected date to be equal to or greater than current date?

If there\'s already a t

3条回答
  •  盖世英雄少女心
    2020-12-05 03:27

    Use [Remote] for special validations, simple and easy:

    Your model:

    [Remote("ValidateDateEqualOrGreater", HttpMethod="Post", 
        ErrorMessage = "Date isn't equal or greater than current date.")]
    public DateTime Date { get; set; }
    //other properties
    

    Your action:

    [HttpPost]
    public ActionResult ValidateDateEqualOrGreater(DateTime Date)
    {
         // validate your date here and return True if validated
         if(Date >= DateTime.Now)
         {
           return Json(true);
         }
         return Json(false);    
    }
    

提交回复
热议问题