29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 25<br />

Model Validation<br />

In the previous chapter, I showed you how the MVC Framework creates model objects from HTTP requests through the model<br />

binding process. Throughout that chapter, I worked on the basis that the data the user supplied was valid. The reality is that users<br />

will often enter data that isn’t valid and cannot be used, which leads me to the topic of this chapter: model validation.<br />

Model validation is the process of ensuring the data received by the application is suitable for binding to the model and, when<br />

this is not the case, providing useful information to the user that will help explain the problem.<br />

The first part of the process, checking the data received, is one of the key ways to preserve the integrity of the domain model.<br />

Rejecting data that doesn’t make sense in the context of the domain can prevent odd and unwanted states arising in the application.<br />

The second part, helping the user correct the problem, is equally important. Without the information and feedback they need to<br />

interact with the application, users become frustrated and confused. In public-facing applications, this means users will simply<br />

stop using the application. In corporate applications, this means the user’s workflow will be hindered. Neither outcome is<br />

desirable. Fortunately, the MVC Framework provides extensive support for model validation. I will show you how to use the basic<br />

features and then demonstrate some advanced techniques to fine-tune the validation process. Table 25-1 provides the summary for<br />

this chapter.<br />

Table 25-1. Chapter Summary<br />

Problem Solution Listing<br />

Explicitly validate a model Use the ModelState object to record validation errors. 1–7<br />

Generate a summary of validation<br />

errors<br />

Display property-level validation<br />

errors<br />

Use the Html.ValidationSummary helper method. 8–10<br />

Use the Html.ValidationMessageFor helper method. 11<br />

Define validation rules within the model<br />

class<br />

Apply attributes to the properties of the model class. 12<br />

Create a custom validation attribute Derive from the ValidationAttribute class. 13–19<br />

Define a self-validating model Implement the IValidatableObject interface. 20<br />

Use client-side validation Add the Microsoft unobtrusive validation package to the application. 21–23<br />

Perform remote validation<br />

Define an action method that returns a<br />

property to be validated.<br />

JsonResult and add the Remote attribute to the model<br />

24, 25<br />

Preparing the Example Project<br />

For this chapter, I created a new project called ModelValidation using the Visual Studio Empty template, checking the<br />

option to add the core MVC folders and references. Having created the project, I added a new class file called<br />

Appointment.cs, which you can see in Listing 25-1, to the Models folder.<br />

Listing 25-1. The Contents of the Appointment.cs File<br />

using System;<br />

using System.ComponentModel.DataAnnotations;<br />

namespace ModelValidation.Models {<br />

662

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!