Model Binding is the process of creating a. NET object with data that the browser sends in an HTTP request.
The parameters of the action method depend on the model binding process (implemented through the model binder). The process of constructing the parameter objects required by the action method using the data that the entire HTTP request carries (the data that the user enters in the form, the routing data, the query string in the request URL, the file that is uploaded in the request).
Model binding process (what the model Binder will do):
1. Detect the name and type of the target object (the object to be created, the parameter of the action method); ( the name and type of the action method parameter )
2. Find the data source (request) by object name and find the available data (usually a string); ( find string data with name matching in the request data )
3. Converts the found data value to the target type based on the object type; ( converts the string type data to the type of the action method parameter )
4. Construct the target object by object name, object type, and such processed data; ( an object using the above data to construct an action method parameter )
5. Pass the constructed object to the action Invoker and inject the object into the target action method by the action Invoker. ( passing the target object to the action method )
Caller: Iactioninvoker
Default caller: Controlleractioninvoker
Model Binder: Imodelbinder
Default model binder: Defaultmodelbinder
Requests: Request
Request data: Request.Form, Request.Files, Routedata.values, Request.QueryString
Models: Model
Model Properties: Model.name
Property Type: String
Controllers: Controller
Action method: Action
Action parameter: Actionparam
Parameter type: string
Parameter value: "AA"
First, the default model binder
The Defaultmodelbinder class finds the order of parameter data (stops if found)
| Data source |
Describe |
Example |
| Request.Form |
data that is provided (filled in) by the user in the HTML form |
request.form["id"] |
| Routedata.values |
Values obtained with application routing |
routedata.values["id"] |
| Request.QueryString |
Part of the data contained in the query string in the request URL |
request.querystring["id"] |
| Request.Files |
Files uploaded in the request |
request.files["id"] |
1. Composite type binding : A property (Simple type: string, int ...) when the argument of an action method is a composite type (cannot be converted with the TypeConverter class. ), use reflection to get the public property set, and then bind in turn.
(1) Easy-to-bind HTML:
<div> @Html. labelfor (M = m.homeaddress.city) and m.homeaddress.city) </ Div>
<div><label for= "Homeaddress_city" > City </label>
<input class= "Text-box single-line" id= "homeaddress_city" name= "homeaddress.city" type= "text" value= ""/>
</div>
(2) Specifying a custom prefix:
Data with a binding prefix of "homeaddress"
Public " homeaddress " ) {]addresssummary summary ) {return View (summary); }
(3) To selectively bind attributes:
Exclude bindings for the country property
Public " homeaddress " " Country " ) {]addresssummary summary ) {return View (summary); }
Specifying the properties of a binding
When this class is bound, only the city property is bound, excluding other properties
" City " )] publicclass addresssummary { publicstring getset;} Public string Get Set ; } }
Second, manual call model binding
1.UpdateModel (Parameter list) method:
Public ActionResult Address (ilist<addresssummary> addresses) { new list< Addresssummary>(); Updatemodel (addresses,new// Restrict binding source to form content return View (addresses); }
Value provider: Ivalueprovider
Built-in Ivalueprovider implementation
| Data source |
Ivalueprovider implementation |
| Request.Form |
Formvalueprovider |
| Routedata.values |
Routedatavalueprovider |
| Request.QueryString |
Querystringvalueprovider |
| Request.Files |
Httpfilecollectionvalueprovider |
Improvement: The most common limitation is to find only the form values:
Public actionresult Address (formcollection formData) { IListnew list<addresssummary>(); Updatemodel (addresses, formData); return View (addresses); }
2. Handling Binding Errors
Note: binding errors do not emit an abnormal signal when model bindings are called automatically. The results must be checked through the Modelstate.isvalid property.
InvalidOperationException exception indicates binding error
public ActionResult Address ( FormCollection formData) {IList <AddressSummary> addresses = new list<addresssummary> (); if (TryUpdateModel (addresses, FormData ) { // todo: Bind normal Operation { // todo: Bind exception Action return View (addresses); }
III. Custom Model Binding system (Imodelbinder)
1. Custom Value Providers
The most valuable attribute defined by the Modelbindingcontext class
| Property |
Describe |
| Model |
If the binding is called manually, you can return the model object passed to the Updatemodel method |
| ModelName |
Returns the name of the model being bound |
| Modeltype |
Returns the type of the model being created |
| Valueprovider |
Returns the Ivalueprovider implementation that can be used to fetch data values from the request |
Tip: Modelbinder annotation properties and custom model binders to change the default model binder.
22nd Chapter Model Binding