Hi guys,
I've got a view model that implements IValidatableObject for some DateTime fields that are used with the DateTimePicker. If the validation fails in the Validate method for the DateTime fields they return the error but after fixing the issue (for example DateTime cannot be in the past) the view model and control continues to use a empty/default DateTime value (1-1-1901 12:00 AM) and there's no way to get the model back to a valid state.
The attached image is the form at start. The two DateTime fields are set to the current time. One you submit the form the Validate method kicks in and recognizes the date in the past and throws back the error message. The second image shows the error message in the form. The third image shows the corrected value to be a time in the future (using the time picker). The fourth image shows the form after the POST where the time gets reset back to 1/1/1901 12:00 AM and throws the error it can't be in the past. No matter how many times you change this field it keeps throwing this error.
Here's the Validate method:
1.
public
IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
2.
{
3.
if
(OutageStart < DateTime.Now)
4.
{
5.
yield
return
new
ValidationResult(
"Outage Date / Time cannot appear in the past"
,
new
[] {
"OutageStart"
});
6.
}
7.
}