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: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + v6q49s3m0KajrMrHv7TSu8/large + bj4tPO80sH0uPbToc/large + 0sMmhozwvcD4KPHA + small = "http://www.2cto.com/uploadfile/Collfiles/20140710/20140710090258219.png" 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
Source: http://blog.csdn.net/jinyuan0829
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.