Chapter 15. Validation
This chapter covers
- Implementing Data Annotations
- Extending the ModelMetadataProvider
- Enabling ASP.NET Ajax client-side validation
The ASP.NET MVC 1.0 release provided a lot of out-of-the-box functionality, but one common piece was missing: user input validation. Integrating validation frameworks with the 1.0 release was quite difficult, because the hooks to put in validation weren’t fully formed. With ASP.NET MVC 2 comes full support for validation frameworks, as well as built-in support for Microsoft’s Data Annotations library.
Many web applications require some level of easy validation from the initial login screen. In this chapter, we’ll examine the built-in validators provided in the Data Annotations library. Then we’ll look at extending the model metadata providers with richer, more convention-driven behavior. Finally, we’ll describe how to enable client-side validation support.
Data Annotations, introduced with the .NET 3.5 SP1 release, are a set of attributes and classes defined in the System.ComponentModel.DataAnnotations assembly that allow you to decorate your classes with metadata. This metadata describes a set of rules that can be used to determine how a particular object should be validated.