Take a look at Fluent Validation or MVC Foolproof Validation: those can help you a lot.
With Foolproof for example there is a [GreaterThan("StartDate")]
annotation than you can use on your date property.
Or if you don’t want to use other libraries, you can implement your own custom validation by implementing IValidatableObject
on your model:
public class ViewModel: IValidatableObject
{
[Required]
public DateTime StartDate { get; set; }
[Required]
public DateTime EndDate { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (EndDate < StartDate)
{
yield return new ValidationResult(
errorMessage: "EndDate must be greater than StartDate",
memberNames: new[] { "EndDate" }
);
}
}
}