I am having difficulties getting this to work. I want to allow a user to choose start date
and end date
, if the start date
is greater than the end date
I want an error message to display. I am using MVC
. This is the code I have on my model.
public class ModelClass : IValidatableObject
{
[Required(ErrorMessage = "ID Number is required")]
[Display(Name = "ID Number:")]
[RegularExpression(@"^(\d{13})$", ErrorMessage = "Enter a 13 digit ID number")]
public Int64 ID_Number { get; set; }
[Required(ErrorMessage = "The start date is required")]
[Display(Name = "Start Date:")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}")]
public DateTime Start_Date { get; set; }
[Required(ErrorMessage = "The end date is required")]
[Display(Name = "End Date:")]
[GreaterThan("Start_Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}")]
public DateTime End_Date { get; set; }
IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
if (End_Date < Start_Date)
{
yield return new ValidationResult("EndDate must be greater than StartDate");
}
}
}