In the development of the model layer in ASP. MVC5, the data annotations used have three functions:
- Data mapping (Maps the class of the model layer to the corresponding table using EntityFramework)
- Data validation (validation of data on server side and client)
- Data display (displays the corresponding data in the view layer)
The following namespaces relate to data annotations:
- System.ComponentModel.DataAnnotations
- System.ComponentModel.DataAnnotations.Schema
The DataAnnotations namespace contains the main data annotations, the schema namespace contains data annotations for some data maps, and the MVC namespace contains data annotations for characters, arrays, numeric lengths, and attribute comparisons. The security namespace contains the Membershippassword annotation attribute.
Data mapping and validation-related data annotations:
[Required] |
Required Fields |
[MaxLength] |
Specifies the maximum length of the array or string data allowed in a property |
[MinLength] |
Specifies the minimum length of the array or string data allowed in the property |
[Stringlength] |
Specify minimum and maximum character lengths |
[Range] |
Specify a range of values |
Data validation-related data annotations :
[Remote] |
Using JQuery to verify the properties of a plug-in remote validator |
[FileExtension] |
Verify file name extensions |
[Compare] |
Compare the values of two properties |
[RegularExpression] |
Using regular expression validation |
[CustomValidation] |
Custom Validation Methods |
[DataType] |
Specifies the name of the additional type to associate with the data field |
[EmailAddress] |
e-mail address (equivalent to [DataType (Datatype.email)]) |
[Phone] |
Telephone (IBID.) |
[CreditCard] |
Credit card number (IBID.) |
[URL] |
Verify URL (ibid.) |
[Membershippassword] |
Verify that the password field meets the current password requirements of the membership provider |
Data map-related data annotations:
[Key] |
Primary key field |
[Column] |
Database Column Property Mappings |
[Notmapped] |
Do not create a corresponding field |
[Table] |
Specifies the database table to which the class will be mapped |
[ForeignKey] |
Represents a property that is used as a foreign key in a relationship |
[Databasegenerated] |
Specify how the database generates property values (EF does not track changes in attributes) |
The data shows the relevant data annotations:
[DisplayName] |
Specify localized strings (idioms Class) |
[Display] |
Specify localized strings (idioms properties) |
[DisplayFormat] |
Format a data field |
[ReadOnly] |
Specifies whether the attribute to which the attribute is bound is a read-only property or a read/write property |
[EditAble] |
Indicates whether the data field can be edited |
[Hiddeninput] |
Indicates whether the property value or field value should be rendered as a hidden INPUT element |
[Scaffoldcolumn] |
Specifies whether a class or data column uses scaffolding |
[UIHint] |
Specify the template that dynamic Data uses to display data fields |
Other
[DisplayColumn] |
Specify columns that are displayed in the referenced table as foreign key columns |
[Description] |
A visual designer can display a specified description when referencing a component member (namespace: System.ComponentModel.DescriptionAttribute) |
1. Data validation related data annotations inherit the Validationattribute class, and there is a ErrorMessage property to display the error prompt.
such as: [Required (errormessage= "This item cannot be empty")].
2. The int and datetime types in the data map are not allowed to be null by default in the database, and if you need to set to NULL, you can use nullable types (using int? or DateTime?). )。
Beginners ASP. NET MVC, by the way, summarizes the data annotations (does not include all the built-in data annotations), the specific use of each data annotation, please consult MSDN, inappropriate place to correct.
Annotations on the development data of model layer in MVC5