I have a ViewModel for my MVC4 Prject containing two DateTime properties:
[Required]
[DataType(DataType.Date)]
public DateTime RentDate { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime ReturnDate { get; set; }
Is there a simple way to use C# attributes as [Compare("someProperty")]
to check weather the value of RentDate property is earlier than the value of ReturnDate?
It looks like you're using DataAnnotations so another alternative is to implement IValidatableObject
in the view model:
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (this.RentDate > this.ReturnDate)
{
yield return new ValidationResult("Rent date must be prior to return date", new[] { "RentDate" });
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With