Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Expand]WinForms Controls
[Collapse]ASP.NET Controls and MVC Extensions
 [Expand]Prerequisites
 [Expand]What's Installed
 [Expand]Common Concepts
 [Expand]ASP.NET WebForms Controls
 [Collapse]ASP.NET MVC Extensions
  [Expand]Product Information
  [Expand]Getting Started
  [Expand]Common Concepts
  [Expand]Grid View
  [Expand]Card View
  [Expand]Vertical Grid
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]HTML Editor
  [Expand]Image and Data Browsing
  [Expand]Docking and Popups
  [Expand]Navigation and Layout Extensions
  [Expand]File Management
  [Expand]Multi-Use Site Extensions
  [Expand]Charts
  [Expand]Pivot Grid
  [Expand]Scheduler
  [Expand]Tree List
  [Collapse]Data Editor Extensions
    Editor List
    Main Features
   [Collapse]Common Concepts
     Binding Data Editors to Data
     Getting Editor Values
     Mask Editing
    [Collapse]Validation
      Validation Overview
      Model Validation
      Unobtrusive Client Validation
      jQuery Client Validation
      Built-in Validation
      Remote Validation
      Displaying Validation Error Messages
      Validating Dynamically Loaded Forms
     Accessibility Support
   [Expand]Editor Types
   [Expand]Strongly-Typed Editor Types
  [Expand]Report Extensions
  [Expand]SpellChecker
 [Expand]Localization
 [Expand]Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Model Validation

DevExpress MVC data editors support a model-based data validation approach which is implemented in ASP.NET MVC 2 and 3. This approach is based on decorating model class properties with the DataAnnotations attributes. You can learn more about model validation from this blog post: ASP.NET MVC 2: Model Validation.

This topic describes how to add a validity check to the DevExpress MVC data editors.

To implement user-input validation within DevExpress MVC Data Editors, you need to perform the following steps.

Expanded Adding Validation Rules to the Model Class

To define validation rules for the user input, add the required DataAnnotation attributes to the respected model class properties. Server-side validation is automatically enabled when data annotations are added. You can learn more about DataAnnotation attributes usage in ASP.NET MVC validation from this blog post: How data annotations for ASP.NET MVC validation work.

The code sample below demonstrates how the DataAnnotation attributes can be added to the model class properties.

Expanded Validation-Specific Settings for Data Editors

Displaying Validation Error Messages.

You have the ability to use different approaches to display validation error messages: standard ValidationMessageFor() and ValidationSummary() methods, the DevExpress MVC editors built-in placeholder and DevExpress MVC Validation Summary. You can find these approaches described here: Displaying Validation Error Messages.

Additional requirements.

Note that the editor's Name property value must match the related data model class property name.

The ASP.NET MVC Framework requires your input data editors to be inside of a <form> element that should be rendered with Html.BeginForm() in order to be validated.

Example.

The code sample below demonstrates how you can configure the TextBox extension to render the validation error message within a built-in placeholder.

You can see the rendered result in the picture below.

Expanded Enabling Client-Side Validation

After the previous steps, your application only performs server-side validation: end users will need to submit a form to the server before they will see a validation error messages.

In addition to server-side validation, you can enable client-side validation functionality.

The client-side validation requires MicrosoftAjax.min.js, MicrosoftMvcAjax.min.js and MicrosoftMvcValidation.min.js JavaScript references to be included in your layout or master page (or to a certain views that contain validating forms).

The code sample below demonstrates how to include the required javascript references.

Note

The JavaScript references should be added in the order they are specified in the code sample above.

To enable client-side validation for all pages in your project, add a key to web.config.

To enable the client-side validation functionality for a specific view only, you can add the following code to the required view.

Note

The model validation script parses loaded DOM searching for forms that contain validation metadata. The parsing is only done after the initial page load, so the forms that were loaded via callbacks after the page load are not parsed. See this topic to learn how to validate forms that were loaded via callbacks: Validating Dynamically Loaded Forms.

Expanded See Also

How would you rate this topic?​​​​​​​