ASP. NET does not have the magic _ ASP. net mvc model verification method, _ asp. netmvc

Source: Internet
Author: User

ASP. NET does not have the magic _ ASP. net mvc model verification method, _ asp. netmvc

The previous article introduced the user registration and login functions. When registering a user, you can use code to restrict the format of the user name and password. If the user does not meet the requirements, the operation cannot be completed, for example:

This function is based on the error message provided by the Entity Framework component of Identity after the user submits the data for verification before the user is added.

The data verification function is necessary in every software system. To avoid system errors caused by invalid or invalid data input, verify the data before it is processed or persisted to ensure that the data is correct and valid.

This article will introduce the model verification mechanism in ASP. net mvc from the following points:

● Input verification

●. NET verification Overview

● ASP. net mvc backend Model Verification

● ASP. net mvc client model verification

● Client verification of ASP. NET MVC

● Customize model verification in ASP. NET MVC

Input verification

The purpose of input verification is to determine whether a variable meets the required requirements.Judgment"The most direct way to use a program to implement the current situation is to complete the process by judging the statement, for example:

But does. Net provide a unified method for data verification? Otherwise, the Verification Code cannot be reused and the reading is affected each time by the judgment statement.

About. NET Authentication

In. Net, a feature-based data type tag (DataAnnotations) and verification mechanism are provided.

1. Data Type mark:

. Net has a class library named system. componentmodel. dataannotations, which contains many attributes ). These features are used to mark the data type information of attributes of the. Net type. For example, the Data Length, format, and whether the data can be empty.

For some types in this class library, you can see such as credit card, email address, Maximum/minimum length, and other feature types:

Data Annotation attributes has three types:

● Verification features: used to execute verification rules, such as email address, data type, data length, number range, and regular expression verification features.

● Display feature: used to specify how the marked type or attribute is displayed on the UI.

● Model link feature: used to specify the relationship between class members and other types, such as foreign key features.

Is the result of marking the name attribute of an object type through the feature. From the tag name, we can easily see that the name attribute is required and the maximum length is 30, the displayed name is "name ".

However, it should be noted that the system. componentmodel. dataannotations only provides a description. It does not assign a character string that exceeds the limit because you have limited the length of a character string. It must manually call the verification method or in ASP.. net mvc and EF. (See: https://stackoverflow.com/questions/6496705/how-do-data-annotations-work)

For more information about the feature types provided by dataannotations, see: https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx

2. Data Type Verification

After the data feature mark is used to mark the type, a special verification type is required to complete the verification. This type also exists in system. componentmodel. in the dataannotations dataset, the name is Validator, which is the definition of Validator. From its description, this class is used to verify the types, attributes, and Methods marked with the ValidationAttribute feature:

 

3. type verification example

A Console program is used to demonstrate how to use data verification in. Net:

First, create a Console program, add a reference to the System. ComponentModel. DataAnnotations assembly, and then write the following code:

The following error message is output after running:

After adding the display feature:

The verification prompt is optimized:

4. custom authentication method:

The built-in data verification features of the system are inherited to the ValidationAttribute type. The following describes how to implement data verification by inheriting this type:

First, add a type that inherits ValidationAttribute and override its IsValid method. This method contains the data verification logic and sets the corresponding error information:

Then apply this feature to the corresponding class attributes:

Verification Result:

For custom verification features, see: https://msdn.microsoft.com/en-us/library/cc668224.aspx

ASP. net mvc model verification

ASP. net mvc provides the feature-based model verification function, as long as the corresponding data verification feature is added to the type attribute used for the action parameter, ASP. net mvc automatically verifies the marked attributes when binding the model. The verification result is reflected by the IsValid attribute of ModelState In the Controller.

The following describes how to add data verification for the Model in ASP. net mvc:

1. Mark attributes with the feature "Model", such as mandatory, password type, and comparison between attributes:

2. Add ModelState. IsValid to the Action code to judge. When an error occurs, use the AddModelError method of the ModelState object to carry the error information to the View:

3. Use the Html. ValidationSummary method in View to output verification information:

Note: If the View contains code for client verification, comment out @ Scripts. Render ("~ /Bundles/jqueryval "). This file is used for client verification. Client verification is described later.

4. Use Html objects on the page to generate corresponding html tags based on the model

5. Run the program

Enter an empty username and two unmatched passwords. Click Submit:

Show Verification Failed:

Content output on the page:

6. Use the display feature to optimize the output of attribute names:

Execution result:

Client model verification of ASP. NET MVC

ASP.. net mvc server model verification. This method is used when data is submitted to the server. Although it can filter invalid requests before the business logic, it still needs to send requests to the server, when there are too many requests, these invalid requests will occupy a large amount of server resources. Therefore, if the client can complete the corresponding verification, the response speed is improved for the customer, for the server, the pressure is reduced, ASP. net mvc works with the Jquery Validation plug-in to provide the browser-side data verification function.

Next, on the basis of the above Code, through the introduction of verification-related javascript in the page to achieve verification on the client:

1. Add the following code to introduce verification-related JavaScript code:

2. Use the ForXXX method of the Html object to generate the Html code for the Model tag on The View:

3. Running effect:

The result is the same as that of the server. However, when you click the register button, the Post request is no longer sent to the server. Instead, the js Code is used for data verification in the browser.

About ASP. net mvc client Validation Jquery Validation

Jquery Validation is a Jquery plug-in that provides powerful form data verification functions, allowing you to easily and quickly add some common data verification for Web applications, such as required, number, email address, phone number, regular expression, and other common format verification. Its verification function is completed by adding some attributes to the form tag or setting its type, as shown in. The code is from the official documentation, it provides verification of the length limit, required, Email format, and Url format:

  

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.