ASP. NET has no magic-binding and parsing ASP. net mvc model (Part 1), asp. netmvc

Source: Internet
Author: User

ASP. NET has no magic-binding and parsing ASP. net mvc model (Part 1), asp. netmvc

The previous article introduced ASP. net mvc model binding and verification functions, in line with ASP. net mvc has no magic spirit. In this chapter, we will discuss ASP from the perspective of code.. net mvc has learned how to bind and verify the model.

The main contents of this article include:
● ModelBinder
● ValuePrivoder
● ModelMetadata
● Simple and complex models
● Summary

ModelBinder

ModelBinder is the core component used by ASP. net mvc for model binding. All ModelBinder implements the IModelBinder interface, for example:

  

This interface has only one method, that is, model binding is completed based on the controller and the binding context.
There are different ModelBinder in ASP. net mvc, which are used to bind different types of data, such as common. Net objects and files uploaded over HTTP.
By default, the following five types of ModelBinder are available:
● Defamodelmodelbinder: the default model binder. Generally, requests submitted from the browser are bound to the model using the default processor.
● HttpPostedFileBaseModelBinder: HTTP File model binding
● ByteArrayModelBinder: bind binary data.
● LinqBinaryModelBinder: bind the request to the System. Data. Linq. Binary object. Reference: http://stephenwalther.com/archive/2009/02/25/asp-net-mvc-tip-49-use-the-linqbinarymodelbinder-in-your
● CancellationTokenModelBinder: provides a mechanism for canceling model binding.

All ModelBinder are managed by a dictionary named ModelBinderDictionary, which exists in the definition of the Controller type, as shown in. It is a protected internal attribute, used to complete model binding during Controller execution:

  

ModelBinderDictionary definition:

  

From the definition of ModelBinderDictionary, we can see that it implements a dictionary interface and a set interface that uses the Type as the Key and the IModelBinder Type as the value. You can dynamically increase or decrease ModelBinder according to the ModelBinder Type, in addition, there is a DefaultBinder, which generally runs the following instance:

  

The ModelBinder dictionary in Controller contains the five ModelBinder described above.

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.