0% found this document useful (0 votes)
62 views3 pages

Data Annotation Attributes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views3 pages

Data Annotation Attributes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Register below script on Page (.

cshtml)

@section Scripts {@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }}

Data Annotation Attributes in ASP.NET MVC Core

Ref: https://www.completecsharptutorial.com/asp-net-
mvc5/data-annotation-validation-with-example-in-asp-net-
mvc.php

Here are the numerous types of Data Annotations with the syntax:

1) DataType

This attribute is used to specify the data type of the model.

Syntax

[DataType(DataType.Text)]

2) Required

This attribute defines the particular value as mandatory for a certain requirement

Syntax

[Required(ErrorMessage="Please enter your name"),MaxLength(50)]

3) StringLength

Using this annotation attribute, we can set the maximum and minimum string range of the
property.

Syntax
[StringLength(50,ErrorMessage="Please do not enter values over 50 characters")]

4) MaxLength

With this annotation attribute, we can set the maximum length of the property.

Syntax

[MaxLength(5)]

5) Range

We can apply the Range annotation attribute to define the range between two numbers.

Syntax

[Range(50,250,ErrorMessage="Please enter the correct value")]

6)Bind

This annotation attribute defines fields to enter or eliminate for model binding.

Syntax

[Bind(Exclude = "EmployeeID")]

7) DisplayFormat

This annotation attribute enables us to set the date format defined as per the attribute.

Syntax

[DisplayFormat (DataFormatString = "{0:MM.DD.YYYY}")]

8) DisplayName

With this annotation attribute we can set the property names that will display at the view.

Syntax
[Display (Name="Employee Name")]

9) RegularExpression

Using this attribute, we can set a regex (regular expression) pattern for the property. For example,
Email ID.

Syntax

[RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Your Email


is not valid.")]

You might also like