Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC ModelBindingContext class-- how are its model values populated?

I'm scratching my head a bit at how model binders do their work in ASP.Net MVC.

To be specific, the BindModel() method has a ModelBindingContext parameter that holds the model name and type, but I don't understand how the ModelBindingContext receives these values.

An MVC model has to be populated from posted form values or query string parameters, or other sources of data. But what mechanism determines the model type handed to the ModelBindingContext, and how is one model type chosen over another model type, over even (say) a simple list containing the individual posted values?

It just appears to me the ModelBindingContext "knows" the type of model it's being handed, and I'm not sure where that's coming from or the workflow involved in populating it.

like image 521
larryq Avatar asked Aug 19 '10 16:08

larryq


People also ask

How model binding works in MVC?

How Model Binding Works. Model binding is a simplistic way to correlate C# code with an HTTP request. The model binding applies to transforming the HTTP request data in the query's form string and form collection of the action method parameters. We can consider these parameters to be primitive type or complex type.

What is custom model binding in MVC?

In the MVC pattern, Model binding maps the HTTP request data to the parameters of a Controllers action method. The parameter can be of a simple type like integers, strings, double etc. or they may be complex types. MVC then binds the request data to the action parameter by using the parameter name.

What is model binding in asp net core?

Model binding allows controller actions to work directly with model types (passed in as method arguments), rather than HTTP requests. Mapping between incoming request data and application models is handled by model binders.

What is model binding in Web API?

The model binding system: Retrieves data from various sources such as route data, form fields, and query strings. Provides the data to controllers and Razor pages in method parameters and public properties. Converts string data to . NET types.


1 Answers

Interesting question. Here is a simple overview of what MVC does. It's all handled by the ControllerActionInovker class. This is not in specific order, but is close.

  1. ControllerActionInovker determines the parameter type via reflection.
  2. Next ValueProviders are created from the HttpContext Request Form, Route, QueryString, etc. properties. You can also provide your own value providers.
  3. These ValueProviders are supplied to a ModelBindingContext via a collection that acts as a virtual ValueProvider.
  4. Then ControllerActionInovker looks for a ModelBinder for the specific type. If it doesn't find one it defaults to the built in DefaultModelBinder.
  5. In most cases the DefaultModelBinder is used. It's job is to create a Model, and use the ValueProviders to connect the properties with values using the model properties names as a key. When the ValueProviders have a value, they return a ValueProviderResult object that is responsible for type conversion.

You can see this for yourself in the ASP.net MVC source located at codeplex.com. Look for the ControllerActionInvoker class and the GetParameterValue method.

like image 88
Lynn Eriksen Avatar answered Sep 18 '22 17:09

Lynn Eriksen