How to: Add Validation to the Data Model Using Attributes
Introduction
ASP.NET Dynamic Data enables validation of data fields to be delegated to the data layer and removed from the presentation layer. This topic shows how you can add validation to the data model by using attributes.
To run the example code in this topic, you will need the following:
Dynamic Data provides the following attributes for validation purposes:
|
Attribute name
|
Description
|
|
RangeAttribute
|
Specifies that the field value must be in a specific numeric range. The first parameter is the field name, the second parameter is the minimum value, and the third parameter is the maximum value.
|
|
RegexAttribute
|
Specifies that the field value must match a specific regular expression. The first parameter is the field name and the second parameter is the regular expression.
|
|
RequiredAttribute
|
Specifies that the field value must not be empty. The first parameter is the field name.
|
In order to add these attributes to the data model, you must create a partial class for the table you want to add validation, and apply the attributes to the class definition.
All these attributes support the ErrorMessage named parameter that enables you to specify a message that is displayed when validation fails. The ErrorMessage value can contain indexed placeholders for the other parameters. The following table shows the possible indexed placeholders values that you can use:
|
Index
|
Description
|
|
0
|
Returns the field name.
|
|
1
|
Returns the minimum value for the Range attribute.
|
|
2
|
Returns the maximum value for the Range attribute.
|
The following example shows how to apply range validation to the UnitsOnOrder field of the Products table. The provided number must be between 0 and 100.
The following example shows how to apply required-field validation to the ContactName field of the Customers table.
The following example shows how to apply regular expression validation to the PhotoPath field of the Employees table. The provided path must be a valid URL.
To create a partial class for a database table
-
In Solution Explorer, right-click the App_Code folder, and then click Add New Item.
-
Under Installed templates, click Class.
In the Name box, enter the name of the database table you want to add validation for, such as Product.
-
Switch to or open the class file that you just created.
-
Add the Partial keyword in Visual Basic or the partial keyword in Visual C# to the class definition to make it a partial class.
-
Add a namespace directive that refers to the System.Web.DynamicData namespace by using the Imports keyword in Visual Basic or the using keyword in Visual C#.
-
If you are using Visual C#, remove the constructor method provided.
To add range validation for a field
-
Apply the Range attribute to the partial class definition.
The following example adds a range validation for the UnitsOnOrder field of the Products table of the Northwind database. It provides an error message that is displayed if validation fails.
App_Code
using System.Web.DynamicData;
[Range("UnitsOnOrder", 0, 100,
ErrorMessage="Units on orders should be between {1} and {2}.")]
public partial class Product {
}
App_Code
Imports System.Web.DynamicData
<Range("UnitsOnOrder", 0, 100, _
ErrorMessage:="Units on orders should be between {1} and {2}.")> _
Partial Public Class Product
End Class
To add required-field validation
-
Apply the Required attribute to the partial class definition.
The following example adds a required-field validation for the ContactName field of the Customers table of the Northwind database. It provides an error message that is displayed if validation fails.
App_Code
using System.Web.DynamicData;
[Required("ContactName",
ErrorMessage = "Contact name is required.")]
public partial class Customer
{
}
App_Code
Imports System.Web.DynamicData
<Required("ContactName", _
ErrorMessage:="Contact name is required.")> _
Partial Public Class Customer
End Class
To add field validation using regular expression
-
Apply the Regex attribute to the partial class definition.
The following example adds format validation for the PhotoPath field of the Customers table of the Northwind database, and provides an error message that is displayed if validation fails.
App_Code
using System.Web.DynamicData;
[Regex("PhotoPath",
@"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$",
ErrorMessage = "Photo path is invalid.")]
public partial class Employee
{
}
App_Code
Imports System.Web.DynamicData
<Regex("PhotoPath", _
"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$", _
ErrorMessage:="Photo path is invalid.")> _
Partial Public Class Employee
End Class
This topic is ASP.NET Extensions documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases.