concept DataAnnotations in category asp.net

appears as: DataAnnotations, DataAnnotations
ASP.NET Core in Action, Second Edition

This is an excerpt from Manning's book ASP.NET Core in Action, Second Edition.

Definition

As an example, let’s consider the checkout page for your currency converter application. You need to collect details about the user before you can continue, so you ask them to provide their name, email and, optionally, a phone number. The following listing shows the UserBindingModel decorated with validation attributes that represent the validation rules for the model. This expands on the example you saw in listing 6.4.

Listing 6.6 Adding DataAnnotations to a binding model to provide metadata
public class UserBindingModel
{
    [Required]                                        //#A
    [StringLength(100)]                               //#B
    [Display(Name = "Your name")]                     //#C
    public string FirstName { get; set; }
 
    [Required]                                        //#A
    [StringLength(100)]                               //#B
    [Display(Name = "Last name")]                     //#C
    public string LastName { get; set; }
 
    [Required]                                        //#A
    [EmailAddress]                                    //#D
    public string Email { get; set; }
 
    [Phone]
    [Display(Name = "Phone number")]                  //#C
    public string PhoneNumber { get; set; }
}

Suddenly your binding model contains a whole wealth of information where previously it was pretty sparse on details. For example, you’ve specified that the FirstName property should always be provided, that it should have a maximum length of 100 characters, and that when it’s referred to (in error messages, for example) it should be called "Your name" instead of "FirstName".

ASP.NET Core in Action

This is an excerpt from Manning's book ASP.NET Core in Action.

6.3.2. Using DataAnnotations attributes for validation

Validation attributes, or more precisely DataAnnotations attributes, allow you to specify rules that the properties in your model should conform to. They provide metadata about your model by describing the sort of data the model should contain, as opposed to the data itself.

Listing 6.5. Adding DataAnnotations to a binding model to provide metadata
Public class UserBindingModel { [Required] #1 [StringLength(100)] #2 [Display(Name = "Your name")] #3 public string FirstName { get; set; } [Required] #1 [StringLength(100)] #4 [Display(Name = "Last name")] #3 public string LastName { get; set; } [Required] #1 [EmailAddress] #5 public string Email { get; set; } [Phone] [Display(Name = "Phone number")] #3 public string PhoneNumber { get; set; } } #1 Values marked Required must be provided. #2 The StringLengthAttribute sets the maximum length for the property. #3 Customizes the name used to describe the property #4 The StringLengthAttribute sets the maximum length for the property. #5 Validates the value of Email is a valid email address !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":50},{\"line\":2,\"ch\":52}],[{\"line\":7,\"ch\":50},{\"line\":7,\"ch\":52}],[{\"line\":12,\"ch\":50},{\"line\":12,\"ch\":52}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":2}],[{\"line\":3,\"ch\":50},{\"line\":3,\"ch\":52}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":2}],[{\"line\":4,\"ch\":50},{\"line\":4,\"ch\":52}],[{\"line\":9,\"ch\":50},{\"line\":9,\"ch\":52}],[{\"line\":17,\"ch\":50},{\"line\":17,\"ch\":52}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":2,\"ch\":50},{\"line\":2,\"ch\":52}],[{\"line\":7,\"ch\":50},{\"line\":7,\"ch\":52}],[{\"line\":12,\"ch\":50},{\"line\":12,\"ch\":52}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":2}],[{\"line\":8,\"ch\":50},{\"line\":8,\"ch\":52}],[{\"line\":24,\"ch\":0},{\"line\":24,\"ch\":2}],[{\"line\":4,\"ch\":50},{\"line\":4,\"ch\":52}],[{\"line\":9,\"ch\":50},{\"line\":9,\"ch\":52}],[{\"line\":17,\"ch\":50},{\"line\":17,\"ch\":52}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":2,\"ch\":50},{\"line\":2,\"ch\":52}],[{\"line\":7,\"ch\":50},{\"line\":7,\"ch\":52}],[{\"line\":12,\"ch\":50},{\"line\":12,\"ch\":52}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":2}],[{\"line\":13,\"ch\":50},{\"line\":13,\"ch\":52}],[{\"line\":25,\"ch\":0},{\"line\":25,\"ch\":2}],[{\"line\":4,\"ch\":50},{\"line\":4,\"ch\":52}],[{\"line\":9,\"ch\":50},{\"line\":9,\"ch\":52}],[{\"line\":17,\"ch\":50},{\"line\":17,\"ch\":52}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}]]"} !@%STYLE%@!
Figure 8.3. Tag Helpers hook into the metadata provided by DataAnnotations, as well as the property types themselves. The Validation Tag Helper can even populate error messages based on the ModelState, as you saw in the last chapter on validation.