Data annotation validation is a way to decorate your model with validation attributes. For example:
public class Product{
public int ProductId {get;set;}
[Required]
public string Name{get;set;}
}
In this example the Name property is required and asp.net mvc will run validation on it for you. In fact, it will even set up client side validation in javascript automatically. There are a few built in validators, required, stringlength.
CodeHobo
All-Star
18669 Points
2648 Posts
Re: Validating parameters before executing Action?
Mar 29, 2012 08:04 PM|LINK
Data annotation validation is a way to decorate your model with validation attributes. For example:
public class Product{ public int ProductId {get;set;} [Required] public string Name{get;set;} }In this example the Name property is required and asp.net mvc will run validation on it for you. In fact, it will even set up client side validation in javascript automatically. There are a few built in validators, required, stringlength.
You can read more about data annotation validation here.
http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validation-with-the-data-annotation-validators-cs
In addition to the built in annotations, you can also add your own custom annotation and use that in your model.
Take a look at this post on custom data annotation validation attributes.
http://odetocode.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-code.aspx
Blog | Twitter : @Hattan