Validation and Model State Relation in MVC

The primary side effect of model binding is model state (accessible in a Controller-derived object using the ModelState property). Not only does model state contain all the values a user attempted to put into model properties, but model state also contains all the errors associated with each property (and any errors associated with the model object itself). If there are any errors in modelstate, ModelState.IsValid returns false. As an example, imagine the user submits the checkout page without providing a value for LastName. With the Required validation annotation in place, all the following expressions will return true after model binding … Click here to continue…..

Range Validation in MVC

The Range attribute specifies minimum and maximum constraints for a numerical value. If the Music Store only wanted to serve middle-aged customers, you could add an Age property to the Order class and use the Range attribute as in the following code:

The first parameter to the attribute is the minimum value, and the second parameter is the maximum value. The values are inclusive. The Range attribute can work with integers and doubles, and another overloaded version of the constructor will take a Type parameter and two strings (which can allow you to add a range to date and … Click here to continue…..

RegularExpression Validation in MVC

Some properties of Order require more than a simple presence or length check. For example, you’d like to ensure the Email property of an Order contains a valid, working e-mail address. Unfortunately, it’s practically impossible to ensure an e-mail address is working without sending a mail message and waiting for a response. What you can do instead is ensure the value looks like a working e-mail address using a regular expression:

Regular expressions are an effi cient and terse means to enforce the shape and contents of a string value. If the customer gives you an e-mail address and … Click here to continue…..

StringLength Validation in MVC

You’ve forced the customer to enter his name, but what happens if he enters a name of enormous length? Wikipedia says the longest name ever used belonged to a German typesetter who lived in Philadelphia. His full name is more than 500 characters long. Although the .NET string type can store (in theory) gigabytes of Unicode characters, the MVC Music Store database schema sets the maximum length for a name at 160 characters. If you try to insert a larger name into the database, you’ll have an exception on your hands. The StringLength attribute can ensure the string value provided … Click here to continue…..

Required Validation in MVC

Because you need the customer to give you his fi rst and last name, you can decorate the FirstName and LastName properties of the Order model with the Required attribute:

The attribute raises a validation error if either property value is null or empty. (You will learn how to deal with validation errors in just a bit.) Like all the built-in validation attributes, the Required attribute delivers both server-side and client side validation logic (although internally, it is another, different component in the MVC framework that delivers the client-side validation logic for the attribute through a validation adapter design). … Click here to continue…..