MVC: Custom Model Validation

Please see my other MVC articles.

Using Custom Validation on Model Properties

While ASP.NET MVC provides powerful native validation, some requirements require custom routines to ensure certain model data is valid.

In this artilce I’ll demonstrate how to configure a model for custom validation and provide a simple example function.

First, I’ll decorate the model property with an attribute of the name of my custom validation routine – the first attribute – “ValidateBiennium.”
This instructs MVC when validating the model, for this property call ValidateBiennium().

Next, I’ll provide the custom validation function.

The most important part of my custom validation class is that it inherits from ValidationAttribute. This allows the framework to link your custom valdiation to the model.