Add a property attribute to your model, for example.
[DateInTheFuture] public DateTime ShippingDate { get; set; }
and the attribute is the usual simple validation attribute:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public class DateInTheFutureAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext context) { var futureDate = value as DateTime?; var memberNames = new List<string>() { context.MemberName }; if (futureDate != null) { if (futureDate.Value.Date < DateTime.UtcNow.Date) { return new ValidationResult("This must be a date in the future", memberNames); } } return ValidationResult.Success; } }
source share