If the built in attributes are not sufficient to validate your model data, then you can place your validation logic in a class derived from ValidationAttribute. In this example only odd numbers are valid values for a model member.
Custom Validation Attribute
public class OddNumberAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
try
{
var number = (int) value;
if (number % 2 == 1)
return ValidationResult.Success;
else
return new ValidationResult("Only odd numbers are valid.");
}
catch (Exception)
{
return new ValidationResult("Not a number.");
}
}
}
Model Class
public class MyModel
{
[OddNumber]
public int Number { get; set; }
}