Let’s walkthrough a simple CRUD scenario with ASP.NET MVC 2
that takes advantage of the new built-in DataAnnotation validation
support. Specifically, let’s implement a "Create" form that
enables a user to enter friend data:
We want to ensure that the information entered is valid
before saving it in a database – and display appropriate error messages if it
isn’t:
We want to enable this validation to occur on both the
server and on the client (via JavaScript). We also want to ensure that
our code maintains the DRY principle (“don’t repeat yourself”) – meaning that
we should only apply the validation rules in one place, and then have all our
controllers, actions and views honor it.
Below I’m going to be using VS 2010 to implement the above
scenario using ASP.NET MVC 2. You could also implement the exact same
scenario using VS 2008 and ASP.NET MVC 2 as well.