Validating an index
The Movie Meta Data class contains proxy properties for the properties of the Movie class.The validator attributes are applied to the properties of the Movie Meta Data class.If you submit the form for creating a Product and you do not enter values for the required fields, then the validation error messages in Figure 3 are displayed.Figure 3: Missing required fields If you enter an invalid currency amount, then the error message in Figure 4 is displayed. Add the following line of code to the Application_Start() event handler so that the Application_Start() method looks like this: This line of code registers the ata Annotations Model Binder as the default model binder for the entire ASP. When you use the Data Annotations Model Binder, you use validator attributes to perform validation. The Product class in Listing 1 illustrates how to use these validator attributes. NET Framework Service Pack 1 with the Data Annotations Model Binder. Finally, you need to register the Data Annotations Model Binder in the file. Data Annotations namespace includes the following validator attributes: Note If your validation needs are not satisfied by any of the standard validators then you always have the option of creating a custom validator attribute by inheriting a new validator attribute from the base Validation attribute.In order to use the Data Annotations Model Binder in an ASP. Select Create from the view content dropdown list (see Figure 2).
Because the Entity Framework Designer generates the model classes, any changes you make to the model classes will be overwritten the next time you make any changes in the Designer.
For example, imagine that you have created a Movie class using the Entity Framework (see Figure 5).
Imagine, furthermore, that you want to make the Movie Title and Director properties required properties. Therefore, there is no way to apply the validator attributes to the properties of the Movie class defined in the Data Model.
In that case, you can create the partial class and meta data class in Listing 4. file by applying the validator attributes to the properties of the Movie class defined in the file in Listing 4.
Figure 5: Movie class generated by Entity Framework Listing 4: Models\The file in Listing 4 contains two classes named Movie and Movie Meta Data. It corresponds to the partial class generated by the Entity Framework that is contained in the Data Model. Notice that the Movie partial class is decorated with a Metadata Type attribute that points at the Movie Meta Data class.