MVC 5 Data annotations

Source: Internet
Author: User

In the development of the model layer in ASP. MVC5, the data annotations used have three functions:

    1. Data mapping (Maps the class of the model layer to the corresponding table using EntityFramework)
    2. Data validation (validation of data on server side and client)
    3. Data display (displays the corresponding data in the view layer)

The following namespaces relate to data annotations:

    • System.ComponentModel.DataAnnotations
    • System.ComponentModel.DataAnnotations.Schema
    • System.ComponentModel
    • System.Web.Mvc
    • System.Web.Security

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?). )。

MVC 5 Data annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.