Include Client Side Validation When Using Data Annotations for Validation of Model State
Currently, when using model binding, we can utilize model state validation by adding data annotations to our models. This works nicely as model state errors can be displayed in the ValidationSummary control. This feature requires a round trip to the server, however. I would love to see this extended to include an option to generate client side validation based on the same data annotations.
This is already supported today by way of using Dynamic Data Entity & Field Templates along with Model Binding. Note that despite the name, you don't need to create an entire Dynamic Data site to use these controls (<asp:DynamicEntity>, <asp:DynamicControl>). They can be placed directly in a <asp:FormView>'s template ares (e.g. EditItemTemplate) and by default will render validator controls (including client-side validation) based on Data Annotations on your model.
Graham Mendick commented
Here's a possible implementation, assuming 4.5 data binding is used where the Type of the data item is known at compile time:
1. Change the generated data bound code to use reflection to determine the data annotations for the bound property
2. For each of the annotations found set an appropriate attribute against the bound control, e.g., if a Required annotation is found 'required' (or 'data-required') will be added to the TextBox Control's Atttributes
This approach has many things going for it:
1. The validation rules are maintained in a single place, i.e., the ViewModel
2. Brings Web Forms and MVC validation in line
3. Removes the need for ValidationControls because server side the validation happens as part of Model validation
3. Makes Web Forms validation HTML5 ready where validation attributes are natively supported