Develop data annotation and mvc5model layer annotation based on Model layer in MVC5
The Model layer development in ASP. NET MVC5 uses data annotations for three purposes:
Data ing (map the class at the Model layer to the corresponding table using EntityFramework)
Data verification (verify data validity on the server and client)
Data Display (display corresponding data at the View layer)
Data annotation-related namespaces are as follows:
System. ComponentModel. DataAnnotations
System. ComponentModel. DataAnnotations. Schema
System. Web. Mvc
System. Web. Security
The DataAnnotations namespace contains the main data annotations. The Schema namespace contains some data mappings. The Mvc namespace contains data annotations for character, array, value length, and attribute comparison, the Security namespace contains the MemberShipPassword Annotation Feature.
Data annotation related to data ing and verification:
| [Required] |
Required field |
| [MaxLength] |
Specifies the maximum length of the array or string data allowed in the attribute. |
| [MinLength] |
Specifies the minimum length of the array or string data allowed in the attribute. |
| [StringLength] |
Specify minimum and maximum characters |
| [Range] |
Value Range |
Data annotation related to data verification:
| [Remote] |
Use jQuery to verify the features of the remote verification program |
| [FileExtension] |
Verify File Extension |
| [Compare] |
Compare the values of two attributes |
| [RegularExpression] |
Regular Expression Verification |
| [CustomValidation] |
Custom Verification Method |
| [DataType] |
Name of the additional type to be associated with the data field |
| [EmailAddress] |
Email Address (equivalent to [DataType (DataType. Email)]) |
| [Phone] |
Phone number (same as above) |
| [CreditCard] |
Credit card number (same as above) |
| [Url] |
Verify URL (same as above) |
| [MemberShipPassword] |
Verify that the password field meets the current password requirements of the membership provider |
Data annotation related to data ing:
[Key]Primary key field[Column]Database column property ing[NotMapped]Do not create corresponding fields[Table]Database Table to which the specified class maps[ForeignKey]Attribute used as a foreign key in the Link[DatabaseGenerated]Specify how the database generates attribute values (EF does not track attribute changes)
Data annotations related to data display:
| [DisplayName] |
Specifies the localized string (idioms) |
| [Display] |
Specifies the localized string (idioms) |
| [DisplayFormat] |
Set the Data Field Format |
| [ReadOnly] |
Specifies whether the attribute bound to this feature is read-only or read/write. |
| [EditAble] |
Indicates whether the data field can be edited. |
| [HiddenInput] |
Indicates whether attribute values or field values should be displayed as hidden input elements. |
| [ScaffoldColumn] |
Whether the base frame is used for the specified class or data column |
| [UIHint] |
Template for displaying dynamic data fields |
Others
| [DisplayColumn] |
Specify the columns displayed in the referenced table as foreign key columns. |
| [Description] |
The visualization designer can display the specified description When referencing component members. (Namespace: System. ComponentModel. DescriptionAttribute) |
1. Data annotation inheritance related to data verificationValidationAttribute class, Each hasErrorMessage attributesUsedShow error message.
For example:[Required (ErrorMessage = "this item cannot be blank")].
2. Data ingInt and DateTime typesIn the databaseNULL is not allowed by default., IfSet to NULL,Use an empty type (use Int? Or DateTime ?).
The above data annotation based on the Model layer in MVC5 is all the content shared by the editor. I hope to give you a reference and support for the customer's house.