Skip to content

Data annotations

Description

When you use the Data Annotations Model Binder, you use validator attributes to perform validation. The System.ComponentModel.DataAnnotations namespace includes the following validator attributes:

  • Range โ€“ Enables you to validate whether the value of a property falls between a specified range of values.
  • RegularExpression โ€“ Enables you to validate whether the value of a property matches a specified regular expression pattern.
  • Required โ€“ Enables you to mark a property as required.
  • StringLength โ€“ Enables you to specify a maximum length for a string property.
  • Validation โ€“ The base class for all validator attributes.

Example

public class Product {
    public int Id { get; set; }

    [Required]
    [StringLength(10)]
    public string Name { get; set; }

    [Required]
    public string Description { get; set; }

    [DisplayName("Price")]
    [RegularExpression(@"^\$?\d+(\.\d{2})?$")]
    public decimal UnitPrice { get; set; }
}