The model binding refers to the process of extracting data from a URL and generating parameters corresponding to the action method. The previous series of descriptor is responsible for providing the controller, behavior methods and parameters of the metadata, Valueprovieder is responsible for obtaining data, the rest of the moder binding is to use the results of the two above to achieve the model of the final generation. The binder of the Bindinginfo (type Parameterbindinginfo) inside the Parameterdescriptor is an object that implements the Imodelbinder interface. The Modelbinder component is used for a true model binding.
Modelbinder components
The following looks at the class diagram of the component first
Imodelbinder: Bindmodel method, implements the model binding function method, the incoming ControllerContext and Modelbindingcontext returns the binding complete model.
custommodelbinderattribute: Abstract attribute, complete the binding between model and Modelbinder, the Getbinder method is used to obtain modelbinder.
Modelbinderattribute: The only inheritance of Custommodelbinderattribute, which can be used in parameters, interfaces, enumerations, structures, classes, However, only the parameter attributes of the action method are parsed when the parameterdescriptor is parsed.
Imodelbinderprovider: There is no class in the MVC framework that implements this interface and is primarily used for custom extensions. The Getbinder method is used to get modelbinder by the specified type.
modelbinderprovidercollection: For a collection of Imodelbinderprovider.
modelbinderproviders: Has a read-only attribute binderproviders, which is the modelbinderprovidercollection type.
Modelbinderdictionray: The dictionary collection of Imodelbinder, with the model data type type key, corresponding to the dictionary set of value Modelbinder.
modelbinders: Read-only property with binders, type Modelbinderdictionray, only used to get modelbinderdictionray.
Get the modelbinders mechanism: It is generally first checked from the parameterdescription inside Modelbinder if the existence, if the modelbinder does not exist, is obtained from the Modelbinders.binders property, and is actually obtained from the internal collection _innerdictionary or _modelbinderproviders of the Modelbinderdictionary If it is not available in either of these collections, the default Molderbinder--_defaultbinder in Modelbinderdictionary is used.
In accordance with the above-mentioned acquisition mechanism, when customizing the data type Modelbinder, different ways are used, and the modelbinder gets different priorities:
1) Add Modelbinderattribute or custommodelbinderattribute characteristics to the parameters on the action method;
2) Custom Modelbinderprovider does not have a model data to provide Modelbinder, use the ModelBinderProviders.BinderProviders.Add method to add to the collection
3) Add Modelbinderattribute or custommodelbinderattribute characteristics to the model type
4) Add Modelbinder via Modelbinders's Binders property.
Modelstate
The model binding, in addition to binding the value to the parameters of the action method, is also bound to the controller's ViewData, which lists the modelstate related classes.
Modelbindingcontext
Modelbindingcontext: Has a series of properties from the Parameterdescriptor. The generation and functioning of the modelbindercontext as shown
Summary of ASP. NET MVC model bindings