Trigger IValidatableObject.Validate When ModelState.IsValid is false
I recently came across an ASP.NET MVC issue at work where the validation for my Model was not firing correctly. The Model implemented the IValidatableObject
interface and thus the Validate
method which did some specific logic to ensure the state of the Model (the ModelState
). This Model also had some DataAnnotation
attributes on it to validate basic input.
Long story short, the issue I encountered was that when ModelState.IsValid == false
due to failure of the DataAnnotation
validation, the IValidatableObject.Validate
method is not fired, even though I needed it to be. This problem arose due to a rare situation in which ModeState.IsValid
was initially false but was later set to true in the Controller’s Action Method by some logic that removed errors from the ModelState
.