This means that enabling validation is really easy for common application scenarios, while at the same time still remaining very flexible for more advanced ones. NET MVC 2 that takes advantage of the new built-in Data Annotation 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 Java Script).
NET MVC will create a Person object and automatically map the incoming form input values to it.This is the second in a series of blog posts I’m doing on the upcoming ASP. This blog post covers some of the validation improvements coming with ASP. Validating user-input and enforcing business rules/logic is a core requirement of most web applications. NET MVC 2 includes a bunch of new features that make validating user input and enforcing validation logic on models/viewmodels significantly easier.These features are designed so that the validation logic is 1) Developers can easily take advantage of the Data Annotation validation support built-into the . Data Annotations provide a really easy way to declaratively add validation rules to objects and properties with minimal code.NET MVC 2 project that looks like below: It has four properties (implemented using C#’s automatic property support, which VB in VS 2010 now supports too – woot! We’ll then add a “Friends Controller” controller class to our project that exposes two “Create” action methods.The first action method is called when an HTTP-GET request comes for the URL.NET MVC 2 (enabling better intellisense and compile time checking support): And now when we run the application and hit the URL we’ll get a blank form that we can enter data into: Because we have not implemented any validation within the application, though, nothing prevents us from entering bogus input within the form and posting it to the server.Let’s now update our application to enforce some basic input validation rules.Notice how we’ll get an immediate error message for our missing value without having to hit the server: If we enter some text that is not a legal email the error message will immediately change from “Email Required” to “Not a valid email” (which are the error messages we specified when we added the rules to our Person class): When we enter a legal email the error message will immediately disappear and the textbox background color will go back to its normal state: The nice thing is that we did not have to write any custom Java Script of our own to enable the above validation logic.Our validation code is also still very DRY- we can specify the rules in one place and have them be enforced across all across the application – and on both the client and server.The text elements with the invalid input are highlighted in red, and the validation error messages we specified are displayed to the end user about them.The form is also preserving the input data the user originally entered – so that they don't have to refill anything. To understand this behavior, let’s look at the Create action method that handles the POST scenario for our form: When our HTML form is posted back to the server, the above method will be called.