ASP. NET MVC Model validation (ii)

Source: Internet
Author: User

ASP. NET MVC Model validation (ii) Preface

The previous section demonstrates a simple model validation example, and then mentions in the article where the model validation is located by default in the MVC framework. This article is to solve this problem, and will describe the type of object related to the Modelvalidator type.

Model Validation
    • Model Validation Simple Application Example
    • Modelvalidator using the build process
    • Custom Implementation Defaultmodelbinder Validation
    • customizing Modelvalidatorprovider and Modelvalidator
    • Validationattribute Attribute class use
    • Example implementations of custom Validationattribute attribute classes

Modelvalidator using the build process

First of all, allow me to "steal" a picture, which is a figure in the ASP. NET MVC Model binding (b) in front of me.

Figure 1

Why do you want to "steal" this figure 1, because the model validation process will be somewhere in this diagram, for this meaning I do not elaborate, just to explain the Blue Line part, that is the model binder generation part, we need this part. See 2

Figure 2

Figure 2 is also part of Figure 1, such a step by step decomposition, so that we have a large to small from the far to near the level of feeling, easy to remember and impress. Some of the friends here will say this is the model binding part pull over what's the use? This is useful, take a look at "Imodelbinder.bindmodel ()" This part to the "Get controller Method parameter value" section, that is, the whole model binding process, the theme is model verification and pull binding, no way, Because model validation is done before the model is bound.

When we do not have a custom model binder, the system is implemented by default is the defaultmodelbinder type, then I will look at the defaultmodelbinder type of the implementation of the internal ...

Open a joke, is to look at the internal implementation but not the source code but (Figure 3), so as to leave an impression, interested in the source of their own to see it.

Figure 3

Feel is not a bit of a pit, don't worry everybody, pit who dare not pit you, actually there is a flow here is not shown, here is the model of the binding complex type to execute the internal method. In Bindcomplexmodel) (model validation is done within the implementation of the method, let's take a look at the internal execution of the method (Figure 4).)

Figure 4

To make a long story short, the process of performing model validation is to perform the property-level validation in model and then perform model-level validation.

In the Bindcomplexelementalmodel () method, the Bindproperties () method is executed first, The PropertyDescriptor collection type is traversed inside this method (Figure 4 shows that the propertydescriptorcollection is insufficient), as you can see, SetProperty () The method is the last function to actually validate the Model property (the next space will explain how to implement a custom model binder to perform model validation.)

And in the SetProperty () method to actually perform validation, can be customized, of course, the system default is obtained from the modelvalidatorproviders.providers, all the authentication after the error message (if any) exists the parameters of the Modelbindingcontext type are passed down in this direction.

After validating the model property, the model itself is validated, and the onmodelupdated () method is called, and there is a problem with this method, which is to not work on it and use it internally Compositemodelvalidator Type of instance for validation, However, the final use of the verification is the modelvalidatorproviders.providers in our custom Modelvalidator type or default (here the details of the I did not look at, the approximate process is so if there are errors please correct, thank you).

Finally, we say that the modelvalidator type of registration, first of all the system is not recognized, it may not be "biological". The providers attribute in the system's Modelvalidatorproviders type is modelvalidatorprovidercollection type, so you should see who the system recognizes . Modelvalidator type of "Dad" Modelvalidatorprovider type (after all, not the inheritance relationship), here is not much to say related models, and many of the previous models are similar.

A sample demonstration of the custom implementation Modelvalidator type is described in the following space.


Jinyuan

Source: http://blog.csdn.net/jinyuan0829

This article is copyrighted by the author and Csdn, welcome reprint, but without the consent of the author must retain this statement, and on the article page

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.