ASP. net mvc 3.0 Learning Series-Model in ASP. net mvc 3.0

Source: Internet
Author: User

Series of articles

ASP. net mvc 3.0 Learning Series-Preface

ASP. net mvc 3.0 Learning Series -- Razor and ASP. net mvc 3.0

ASP. net mvc 3.0 Learning Series-Controllers in ASP. net mvc 3.0

ASP. NET MVC
3.0 Learning Series-Dependency Resolution in ASP. net mvc 3.0

This article mainly introduces the following basic points:

1. Introduction:

In ASP. net mvc 3.0, the Model module is greatly enhanced in Validation. The ASP. net mvc development team wrote jQuery for client verification.Jquery. validate. unobtrusive. js, and Remote validation. Just now, I saw a classmate write an article about Remote validation, which is quite detailed. You can take a look.

Http://www.cnblogs.com/serafin/archive/2011/01/25/1944848.html

I will also introduce how to use remote verification in this article.

In general, the major changes to Validation in ASP. net mvc 3.0 are as follows:

A. New attributes B. New interfaces, c. Improved client validation story.

 

2. Validation in MVC 3

Verification is divided into the following parts:

3. Data Annotations

Microsoft's System. ComponentModel. DataAnnotations is a set of good verification methods.

You can see the main classes it contains:

From the methods it contains, we can see that it is mainly used for field verification. Microsoft uses ASP. net mvc or Silverlight to verify the validity of data. Of course, combined with jQuery client verification in ASP. net mvc 3.0, the function of Data Annotation is more perfect.

4. Custom Validation Attributes

In the above namespace System. ComponentModel. DataAnnotations, The validationattivity class exists, and all the attribute type verification methods inherit it.

For example:

In Required and StringLength, let's look at their classes:

In this case, we can customize the Validation class, which also inherits the ValidationAttribute.

The structure of the custom class is as follows:

We now customize a verification to verify whether the input time is within the valid range.

If the input time is not within the first six months of the current month or is greater than the current time by seven days, a prompt is displayed.

 

5. Self Validation models

This method combines ValidationContext and ValidationResult in the model to provide verification.

 

For example, if our model contains a startdate and an enddate field and the input enddate must not be smaller than startdate, we can use the Self validation model validation mode:

Running result:

 

 

The above two verification methods both require post data when the entire Form submits data, which is not acceptable to everyone, so ASP. net mvc team in ASP. net mvc 3 adds the following two verification methods,

Client Validation and Remote Validation.

 

6. Client Validation:

Everyone can see that Microsoft has been very close to the jQuery team in recent years. jQuery is used in the ASP. net mvc project for client verification.

I'm glad that Microsoft finally gave up its own AJAX library .....

Pay attention to the following points when using jQuery AJAX authentication:

1. Add settings in Web. Config:

 

2. Add a js file:

3. Add the following before Html. FormBegain:

 

Here is an example. You can see what is the difference between the html generated by the client after jquery. validate. unobtrusive. mis. js is used:

Authentication information is available in html nodes.

 

7. Custom Client Validation

The client verification method provided by the ASP. net mvc team is also highly scalable.

 

Example:

 

8. remote validation:

I will not explain the remote validation part. The article is long enough .....

Everybody can look at http://www.cnblogs.com/serafin/archive/2011/01/25/1944848.html.

I personally think the principle of remote validation is to Post data to an Action in real time, and then check whether the returned result is empty. If it is not empty, the Validation Message is displayed. JQUery ajax is mainly used.

 

Nick

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.