Data annotations in ASP.NET Core are attributes that you can apply to model properties to define validation rules, control the display format, specify data types, and more. They play a crucial role in both validating user input and controlling how data is displayed in your application. Here are some common data annotations along with examples:
[Required] Data Annotation:
The [Required] attribute indicates that a property must have a non-null value.
[Required(ErrorMessage = "Name is required.")]
public string Name { get; set; }
[StringLength] Data Annotation:
The [StringLength] attribute specifies the minimum and maximum length of a string property.
[StringLength(50, MinimumLength = 3, ErrorMessage = "Name must be between 3 and 50 characters.")]
public string Name { get; set; }
[Range] Data Annotation:
The [Range] attribute is used to validate that a numeric property falls within a specified range.
[Range(1, 100, ErrorMessage = "Price must be between 1 and 100.")]
public decimal Price { get; set; }
[RegularExpression] Data Annotation:
The [RegularExpression] attribute enforces that a string property matches a specific regular expression pattern.
[RegularExpression(@"^[A-Z]{3}\d{3}$", ErrorMessage = "Must be in the format ABC123.")]
public string Code { get; set; }
[EmailAddress] Data Annotation:
The [EmailAddress] attribute validates that a string property contains a valid email address.
The [Compare] attribute checks if two properties have the same value. It's commonly used for confirming passwords.
[Compare("Password", ErrorMessage = "Passwords do not match.")]
public string ConfirmPassword { get; set; }
[Display] Data Annotation:
The [Display] attribute specifies the display name for a property and can be used to customize how the property's name appears in views.
[Display(Name = "Full Name")]
public string Name { get; set; }
[DataType] Data Annotation:
The [DataType] attribute defines the data type of a property, which affects how it's displayed in views.
[DataType(DataType.Date)]
public DateTime BirthDate { get; set; }
[Key] Data Annotation:
The [Key] attribute indicates that a property is the primary key in the database.
[Key]
public int ProductId { get; set; }
These are just a few examples of common data annotations in ASP.NET Core. They help ensure data integrity, validate user input, and control how data is presented in your application, ultimately improving the user experience and data quality.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Data annotations in ASP.NET Core are attributes that you can apply to model properties to define validation rules, control the display format, specify data types, and more. They play a crucial role in both validating user input and controlling how data is displayed in your application. Here are some common data annotations along with examples:
[Required] Data Annotation:
The [Required] attribute indicates that a property must have a non-null value.
[StringLength] Data Annotation:
The [StringLength] attribute specifies the minimum and maximum length of a string property.
[Range] Data Annotation:
The [Range] attribute is used to validate that a numeric property falls within a specified range.
[RegularExpression] Data Annotation:
The [RegularExpression] attribute enforces that a string property matches a specific regular expression pattern.
[EmailAddress] Data Annotation:
The [EmailAddress] attribute validates that a string property contains a valid email address.
[Url] Data Annotation:
The [Url] attribute ensures that a string property contains a valid URL.
[Compare] Data Annotation:
The [Compare] attribute checks if two properties have the same value. It's commonly used for confirming passwords.
[Display] Data Annotation:
The [Display] attribute specifies the display name for a property and can be used to customize how the property's name appears in views.
[DataType] Data Annotation:
The [DataType] attribute defines the data type of a property, which affects how it's displayed in views.
[Key] Data Annotation:
The [Key] attribute indicates that a property is the primary key in the database.
These are just a few examples of common data annotations in ASP.NET Core. They help ensure data integrity, validate user input, and control how data is presented in your application, ultimately improving the user experience and data quality.