Learning ASP. NET 4.5 mvc4.0 (3) with me)

Source: Internet
Author: User

Today, we continue ASP. NET 4.5 MVC 4.0. We were familiar with mvc4.0 updates in vs11 and Win8 two days ago, as well as the basic syntax and several keywords in MVC. After understanding this, we can further understand MVC. I believe many people have some knowledge about the three modules of MVC. Today, let's take a look at the models module. This is a bit similar to the entity class we use in the third layer. The difference is that the attribute here needs to be added with some validation attributes) or some other filters ). In MVC, the verification attribute is mainly contained in system. componentmodel. dataannotations. dll.ProgramSet, so you need to add reference to it.

 

First, let's take a look atCode, And then further understand the verification attributes used in it. The Code is as follows.

1 Public Class Registermodel
2 {
3 [Required]
4 [Display (name = " User Name " )]
5 Public String Username { Get ; Set ;}
6
7 [Required]
8 [Datatype (datatype. emailaddress)]
9 [Display (name = " Email Address " )]
10 Public String Email { Get ; Set ;}
11
12 [Required]
13 [Stringlength ( 100 , Errormessage = " The {0} must be at least {2} characters long. " , Minimumlength = 6 )]
14 [Datatype (datatype. Password)]
15 [Display (name = " Password " )]
16 Public String Password { Get ; Set ;}
17
18 [Datatype (datatype. Password)]
19 [Display (name = " Confirm Password " )]
20 [Compare ( " Password " , Errormessage = " The password and confirmation password do not match. " )]
21 Public String Confirmpassword { Get ; Set ;}

22}

1. requiredattribute attributes

The attribute of this tag is required. If this item is not set in the data returned by the client, the attribute of modelstate. isvalid is false. The error information will be included in the modelstate object, and the client can use@ Html. validationmessagefor to display the error message of Model Property Verification. Let's open the Internet application project that comes with vs11 and use page inspector to debug the program. Double-click the register button to view the verification information, as shown in.

 

The figure shows the verification information of the registration model requiredattrired verification tag. The data-Val-required attribute in the input tag displays severe information, these attributes are extended by Microsoft for jquery and Its Validation extension library. If you are interested in client verification, you can take a look at the "jquery. validate. JS and jquery. validate. unobtrusive. JS ", so you will know how he works.

 

Ii. displayattribute attributes

This attribute is mainly used for the front-end display string. For example, the username attribute in the registration model is displayed as "User Name" on the front-end. It can be seen from the HTML code in the above image. Of course, we can also change it to "User Name", so that English will not be displayed after execution.

 

Iii. stringlengthattribute

This is a validation attribute used to determine the length of the current attribute string. You can set the minimum and maximum values. This is rarely used. The only project I have done is to use it with the password. Other projects generally provide regular expressions for verification.

 

Iv. compareattribute attributes

This verification property is mainly used to compare whether the values of the two attributes are the same. It is generally used for the password and the confirmation password, because the two attributes must be the same.

 

V. regularexpressionattribute 
This attribute will use the regular expression set in the parameter to verify whether the current attribute is valid. This will be more common, such as user registration and so on.

 

Vi. rangeattribute attributes

This attribute is used for range interval verification. It can be a number or an object. You must specify the minimum and maximum values. If it is not a number, you must specify the comparison type.

 

VII. remoteattribute attributes

This attribute implements remote verification. The parameter mainly sets the controller and Method Name of the server. For example, if we want to verify that the username in the registration model has been registered, we can use this remote attribute.

 

Writing this articleArticleIt was found that it basically belongs to the category of 3.0. Some people in the blog park already write better than me, probably because many of them already know it and there is no need to write it out. I received a message in the blogMVC 3.0 series"Thanks to the" lost memories of Youth ". If you don't understand it, you can read his series of articles. In the next article, I will mainly pay attention to the update content of ASP. NET mvc4.0, while some techniques used in 3.0 will be explained.

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.