All docs
V19.2
19.2
19.1
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

Data Annotation Attributes

  • 6 min to read

DevExpress ASP.NET and MVC components support data annotation attributes. You can apply these attributes to classes to specify validation rules, data display mode, set relationships between classes, etc.

Declaration

MVC

Model:

public class RegistrationData {
    [Required(ErrorMessage = "*")]
    [DisplayFormat(NullDisplayText = "First Name")]
    [Display(Name = "Caption")]
    public string FirstName { get; set; }
    [Required(ErrorMessage = "*")]
    [DisplayFormat(NullDisplayText = "Last Name")]
    public string LastName { get; set; }
    ...
}

View:

@Html.DevExpress().FormLayout(settings => {
    ...
    settings.Items.AddGroupItem(m => m.RegistrationData, g => {
        ...
        g.Items.Add(m => m.RegistrationData.FirstName, i => {
            i.Caption = "Name";
            i.NestedExtension().TextBox(s => {
                AssignTextBoxSettings(s);
                s.Properties.ValidationSettings.ErrorText = "*";
            });
        });
        g.Items.Add(m => m.RegistrationData.LastName, i => {
            i.Caption = "Last Name";
            i.NestedExtension().TextBox(s => {
                AssignTextBoxSettings(s);
                s.Properties.ValidationSettings.ErrorText = "*";
            });
        });
        ...
    });    
}).GetHtml()

See also: Binding Data Editors to Data

WebForms

Note

Only the ASPxFormLayout control supports data annotation attributes if you bind the form layout editors to properties with data attributes.

Model:

class TestClass {        
    [Display(Name = "Check it")]
    [CheckBoxRequired(ErrorMessage = "Error text")]
    public bool CheckBoxField { get; set; }
}
<dx:ASPxFormLayout runat="server" ID="fl">
    <Items>
        <dx:LayoutItem FieldName="CheckBoxField">
            <LayoutItemNestedControlCollection>
                <dx:LayoutItemNestedControlContainer>
                    <dx:ASPxCheckBox runat="server" />
                </dx:LayoutItemNestedControlContainer>
            </LayoutItemNestedControlCollection>
        </dx:LayoutItem>
        ...
    </Items>
</dx:ASPxFormLayout>

Data Display Attributes

The tables below list the most common data attributes supported by the DevExpress data controls.

  • DisplayAttribute - Specifies localizable strings for classes and members.

    public class TestClass
    {
        [Display(GroupName = "{Tabs}/Contact", Order = 2), DataType(DataType.PhoneNumber)]
        public string Phone { get; set; }
    }
    
  • DisplayColumnAttribute - Specifies the column that the control displays in the table as a foreign-key column.

    [DisplayColumn("Phone")]
    public class Customer
    {
    ...
    }
    
  • CheckBoxValuesAttribute - Specifies the check box's value.

    class TestClass {       
            [Display(Name = "Check it")]
            [CheckBoxValuesAttribute(AllowGrayed = false)]
            [CheckBoxRequired(ValidStateMode = CheckBoxValidStateMode.Checked, ErrorMessage = "Error text")]
            public bool CheckBoxField { get; set; }
    }
    
  • DisplayFormatAttribute - Specifies a data field's display format.

    public class TestClass
    {
        [DisplayFormat(DataFormatString = "MMMM/yyyy")]
        public DateTime Date { get; set; }
    }
    
  • EditableAttribute - Specifies whether you can edit a data field.

    public class TestClass
    {
    [Editable(false)] 
    public int MyInt { get; set; } 
    }
    
  • KeyAttribute - Indicates that a property or a field is a key.

    private class TestClass {  
        [Key]  
        public string ID { get; set; }  
    }
    
  • MaxLengthAttribute - Specifies a property value's maximum length.

    public class TestClass
    {
        [MaxLength(5)]
        public string Name { get; set; }
    }
    
  • MinLengthAttribute - Specifies a property value's minimum length.

    public class TestClass
    {
        [MinLength(5)]
        public string Name { get; set; }
    }
    
  • PhoneAttribute - Specifies that a data field value is a phone number.

    public class TestClass
    {
        [Phone] 
        public string PhoneNumber { get; set; }
    }
    
  • RegularExpressionAttribute - Specifies that a data field value should match the specified regular expression.

    public class TestClass
    {
        [RegularExpression(@"\S(.*)\S", ErrorMessage = "Error Text")]
        public object FirstName;
    }
    
  • UIHintAttribute - Specifies a template or user control to display a data field.

    public class EditorTemplatesData {
        [UIHint("Mileage")]
        [Range(10, 999, ErrorMessage = "Must be between {1} <br /> and {2}")]
        public float Mileage { get; set; }
    }
    

Data Type Attributes

  • BindableTypeAttribute - Specifies whether it is a bindable type.

    [BindableType(IsBindable = true)]
        class TestClass
        {
            ...
        }
    
  • DataTypeAttribute - Specifies the type of data field's value.

    public class TestClass
    {
        [DataType(DataType.PhoneNumber)]
        public string Phone { get; set; }
    }
    
  • EnumDataAttribute - Allows you to replace numeric enumerator values with corresponding declarations.

    public class TestClass
    {  
        [EnumDataType(typeof(TestObject))]  
        public object TestObject { get; set; }  
    } 
    
  • MetadataTypeAttribute - Allows you to get data annotation attributes from the specified class.

    [MetadataType(typeof(EmployeeMetadata))]
    public partial class Employee {
    }
    public class EmployeeMetadata {
        [Browsable(false)]
        public Int32 Id { get; set; }
    }
    
  • ScaffoldColumnAttribute - Specifies whether a class or data column is hidden.

    public class TestClass
    {
        [ScaffoldColumn(true)] 
        public object ID;
    
        [ScaffoldColumn(false)]  
        public object ProductName;  
    }
    
  • ScaffoldTableAttribute - Specifies whether a class or data table is hidden.

    [ScaffoldTable(true)]
    public class TestClass
    {
        [Display(Name = "Name")]
        public string Name { get; set; }
        ...
    }
    
  • TimestampAttribute - Specifies a column's data type as a row version.

    public class TestClass
    {
        public string Name { get; set; }
        [Timestamp]
        public byte[] CardID { get; set; }
        }
    

Validation Attributes

  • CompareAttribute - Compares the editor's value with another property.

    class TestClass
    {
        [DataType(DataType.Password), Display(Name = "Password")]
        public string Password { get; set; }
    
        [DataType(DataType.Password), Compare("Password",
                ErrorMessage = "The password you entered do not match"),
                Display(Name = "Confirm Password")]
        public string ConfirmPassword { get; set; }
    }
    
  • ConcurrencyCheck - Checks a property for optimistic concurrency.

    public class Customer
    {
        [ConcurrencyCheck]
        public string ContactName { get; set; }
    
        [ConcurrencyCheck]
        public string ContactTitle { get; set; }
    }
    
  • HtmlSettingsAttribute - Specifies the HTML editing settings for the data field value.

    Note

    The HtmlSettingsAttribute class is in effect only for DevExpress ASP.NET MVC extensions.

    public class TestClasss {
        [Required]
        [HtmlSettings(AllowScripts = false, ResourcePathMode = ResourcePathMode.RootRelative, 
        AllowedDocumentType = AllowedDocumentType.HTML5)]
        public string Html { get; set; }
    }
    
  • CreditCardAttribute - Validates a field value as a credit card number.

    public class TestClass {
        [CreditCard]
        public string CreditCardNumber { get; set; }
    }
    
  • CheckBoxRequiredAttribute - Specifies that a check box is required.

    class TestClass {       
            [Display(Name = "Check it")]
            [CheckBoxValuesAttribute(AllowGrayed = false)]
            [CheckBoxRequired(ValidStateMode = CheckBoxValidStateMode.Checked, 
    ErrorMessage = "Error text")]
            public bool CheckBoxField { get; set; }
    }
    
  • CustomValidationAttribute - Specifies a custom validation method.

    public class TestClass
    {
        [CustomValidation(typeof(TestValidator), "ValidateName")] 
        public string Name { get; set; }
    }
    
  • EmailAddressAttribute - Validates a field value as an email address.

    public class TestClass
    {
        [EmailAddress(ErrorMessage = "InvalidEmailAddress")]
        [Display(Name = "Email")]
        public string Email { get; set; }
    }
    
  • FileExtensionsAttribute - Validates file name extensions.

    public class TestClass
    {
        [FileExtensions(Extensions=". jpg,. png,. gif")]
        public string FileName { get; set; }
    }
    
  • DateRangeAttribute - Specifies the date range settings.

    Note

    The DateRangeAttribute class is in effect only for DevExpress ASP.NET MVC extensions.

    public class TestClass
    {
        [DateRange(StartDateEditFieldName = "TimeField", MinDayCount = 0, 
        MaxDayCount = 3, RangeErrorText = "RangeErrorText")]
        public DateTime StartTime { get; set; }
    }
    
  • MaskAttribute - Specifies the mask settings.

    Note

    The MaskAttribute class is in effect only for DevExpress ASP.NET MVC extensions.

    public class Customer
    {
        [Mask("+1 (999) 000-0000", ErrorMessage = "The phone number is not valid!")]
        [Required]
        public string Phone { get; set; }
    }
    
  • RangeAttribute - Specifies the numeric range constraints for the data field value.

    public class TestClass
    {
        [Range(1, 10, ErrorMessage = "Custom error text")]
        public object Length;
    }
    
  • StringLengthAttribute - Specifies the allowed minimum and maximum length of characters in a data field.

    public class TestClass  
    {  
        [StringLength(4, ErrorMessage = "Error Text")]  
        public string FirstName;  
    
        [StringLength(10)]  
        public string CompanyName;  
    } 
    
  • RequiredAttribute - Specifies that a data field value is required.

    public class TestClass
    {
        [Required(ErrorMessage = "Name is required")]
        public string Name { get; set; }
    }
    
  • UrlAttribute - Provides URL validation.

    public class TestClass  
    {  
        [Url]  
        public string NavigateUrl;  
    }