A practical technique for developing data annotations based on model layer in MVC5

Source: Internet
Author: User
The following small series for you to share a MVC5 based on the model layer development data annotations, with a very good reference value, I hope to be helpful to everyone. Let's take a look at it with a little knitting.

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

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 mapping [notmapped] do not create a corresponding field [table] specifies the database table to which the class will be mapped [ForeignKey] represents the property used as the foreign key in the 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 defaultin the database, and if you need to set to null, use nullable types (int?). or datetime? )。

Above this based on the MVC5 model layer Development data annotation is small part to share all the content of everyone, hope to give you a reference!!!

Related recommendations:

Example analysis of the method of using Miniprofiler to monitor MVC performance in ASP. MVC5

MVC5 limit all HTTP must be post-requested

MVC5 The specific code implementation of the drop-down box single-selection binding

Related Article

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.