Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Model layer Development data Annotation in MVC5

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shares with you the content of a sample analysis of Model layer development data annotations in MVC5. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The data annotations used in the development of the Model layer in ASP.NET MVC5 have three functions:

Data mapping (mapping classes in the Model layer to corresponding tables using EntityFramework)

Data validation (verify the validity of the data on the server side and the client side)

Data display (display the corresponding data in the View layer)

The namespaces associated with data annotations are as follows:

System.ComponentModel.DataAnnotations

System.ComponentModel.DataAnnotations.Schema

System.Web.Mvc

System.Web.Security

DataAnnotations namespaces contain major data annotations, Schema namespaces contain some data annotations for data mapping, Mvc namespaces contain data annotations for characters, arrays, numeric lengths, and attribute comparisons, and Security namespaces contain MemberShipPassword annotation features.

Data annotations related to data mapping and validation:

[Required] required field [MaxLength] specifies the maximum length of array or string data allowed in the attribute [MinLength] specifies the minimum length of array or string data allowed in the attribute [StringLength] specifies the minimum and maximum character length [Range] specifies the numeric range

Data comments related to data validation:

[Remote] use jQuery validation plug-in remote validator feature [FileExtension] validation file extension [Compare] compare the values of two properties [RegularExpression] use regular expression validation [CustomValidation] Custom validation method [DataType] specify the name of the additional type to be associated with the data field [EmailAddress] email address (equivalent to [DataType (DataType.Email)]) [Phone] phone (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 comments related to data mapping:

[Key] Primary key field [Column] Database column attribute mapping [NotMapped] do not create corresponding fields [Table] specify the database table to which the class will map [ForeignKey] represents attributes used as foreign keys in relationships [DatabaseGenerated] specify how the database generates attribute values (EF does not track changes in attributes)

Data Notes related to data display:

[DisplayName] specify localized string (idiom class) [Display] specify localized string (idiom attribute) [DisplayFormat] set the format of the data field [ReadOnly] specify whether the attribute to which the attribute is bound is a read-only attribute or a read / write attribute [EditAble] indicates whether the data field is editable [HiddenInput] indicates whether the attribute or field value should be rendered as a hidden input element [ScaffoldColumn] Specifies whether a class or data column uses a base [UIHint] to specify a template for dynamic data to display data fields

Other

[DisplayColumn] specifies the column displayed in the referenced table as a foreign key column [Description]

The visual designer can display the specified description when referencing a component member

(namespace: System.ComponentModel.DescriptionAttribute)

1. Data annotations related to data validation inherit the ValidationAttribute class, and all have an ErrorMessage attribute to display the error prompt.

For example, [Required (ErrorMessage= "this item cannot be empty").

2. Int type and DateTime type in data mapping are not allowed to be NULL by default in the database. If you need to set it to NULL, you can use nullable type (using Int? Or DateTime? ).

Thank you for reading! This is the end of this article on "sample analysis of Model layer development data annotations in MVC5". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report