ASP. net mvc Model verification (2)

Source: Internet
Author: User

ASP. net mvc Model verification (2)
ASP. net mvc Model verification (2)Preface

In the previous article, we demonstrated a simple Model verification example, and then mentioned in the article where is the default location of Model verification in the MVC framework? This article solves this problem and describes the types related to ModelValidator objects.

Model Verification Simple Example of Model Verification ModelValidator use the generation process Customize defamodelmodelbinder for verification Custom ModelValidatorProvider and ModelValidator ValidationAttribute Sample Implementation of the custom ValidationAttribute feature class

ModelValidator use the generation process

First, please allow me to "steal" A picture. This is a picture in ASP. net mvc Model binding (2) in front of me.

Figure 1

Why do you want to "steal" this figure 1, because the Model verification process will be in a certain position in this figure, I will not elaborate on this meaning, just explain the Blue Line Section, that is the part generated by the Model binder. What we need is this part. View 2

Figure 2

The part in Figure 2 is also shown in Figure 1. This step-by-step decomposition gives everyone a sense of attention from big to small, which is easy to remember and leave an impression. Some may say that this is the binding part of the Model. What is the purpose? This is useful. Let's take a look at [IModelBinder. bindModel ()] This part goes to the [get Controller method parameter value] part, that is, the entire Model binding process. The topic is Model verification and binding. There is no way, because Model verification is performed before Model binding.

When we do not have a custom Model binder, The system implementsDefaultModelBinderType. Let me take a look.DefaultModelBinderSpecific implementation of the internal type... <喎?http: kf ware vc " target="_blank" class="keylink"> VcD4KPHA + v6q49s3m0KajrMrHv7TSu8/large + bj4tPO80sH0uPbToc/large + 0sMmhozwvcD4KPHA + small = "" alt = "\">

It seems like it is a bit difficult. Don't worry. No one is afraid of it. In fact, there is another process that is not shown here. It shows the internal method to be executed to bind a complex type Model. In BindComplexModel (Model verification is performed only in the internal implementation of the method. Let's take a look at the internal execution of the method (figure 4 ).

Figure 4

To put it short, the process of performing Model verification is to first perform Attribute-level verification in the Model and then perform Model-level verification.

In the BindComplexElementalModel () method, the BindProperties () method is first executed, and the PropertyDescriptor collection type is traversed within the method (as shown in figure 4, PropertyDescriptorCollection is insufficient ), as you can see, SetProperty () the method is the final function that actually verifies the Model attribute (this method will be discussed in the next section when the user-defined Model binder performs Model verification ).

The real verification in the SetProperty () method can be customized. Of course, the default system is from ModelValidatorProviders. all the error messages (if any) after verification exist.ModelBindingContextType parameters are passed down.

After the Model attribute is verified, the Model itself will be verified and the OnModelUpdated () method will be called. The problem with this method is that it does not work and is used internally.CompositeModelValidatorType instance for verification, but the final verification is still using ModelValidatorProviders. in Providers, we define the ModelValidator type or the default one (here I am not going to take a closer look at the specific details. The general process is like this. If there is any error, please correct it and I am grateful ).

Let's talk about it.ModelValidatorFirst, the system does not recognize the type of registration. It may not be "biological. In the system's ModelValidatorProviders type, the Providers attribute is of the ModelValidatorProviderCollection type. Now you should see who the system recognizes.ModelValidatorThe "dad" ModelValidatorProvider type (after all, it is not an inheritance relationship). Here we will not talk about the relevant models, which are similar to many of the previous models.

For custom implementationsModelValidatorExamples of types will be discussed later.

Author: Jin Yuan


The copyright of this article is shared by the author and CSDN. You are welcome to repost this article, but you must keep this statement without the author's consent.

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: 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.