How MVC maps HTTP request data to action method parameters and custom .NET objects

Introduction

ASP.NET MVC model binding allows you to map and bind HTTP request data with a model. Model binding makes it easy for you to work with form data because the request data (POST/GET) is automatically transferred into a data model you specify. ASP.NET MVC accomplishes this behind the scenes with the help of Default Binder.

In this article we will dicuss how MVC maps HTTP request data directly into action method parameters and custom .NET objects. We know this all is done by MVC automatically behind the scenes, but how? What’s the mechanism which is doing all this? Lets discuss it in detail with an example.

Lets assume we have a Employee Model like this:

public class Employee
{
public string FirstName { get; set; }
}

We have a form like this.

@using (Html.BeginForm())
{
@Html.TextBoxFor(m=>m.FirstName, new { @class = “form-control” })
@Html.TextBox(“name”, new { @class = “form-control” })
@Html.TextBox(“lastname”, new { @class = “form-control” })
}

And an Action like this:

public ActionResult Index(Employee employee, string name)

Image showing how form looks like:

form

On click of the submit button, we can see in the below watch window how the values are automatically passed to action method.

employee

  • The Employee Model automatically gets populated with value of “FirstName”
  • The string parameter named “name” also gets populated with the value of textbox from our UI
  • By using Request.Form[“lastname”] we can get the value of lastname also.

So, the question is how did we got all these values, even though we didn’t wrote a single line of code for that. So, here is the black magic.

Now before the Index action gets called, there are two important components that do some tricks behind the scenes:

  • ValueProviderDictionary
  • DefaultModelBinder

.The ValueProviderDictionary pulls out the values from the HTTP request and stores them as strings. The fetching itself is done in the following order:

  • Request.Form[“name”], if it exists
  • Else, RouteData.Values[“name”], if it exists
  • Else, Request.QueryString[“name”], if it exists
  • Else returns null

Now that the ValueProviderDictionary has extracted the necessary strings from the incoming request, the DefaultModelBinder class takes on the responsibility of converting these strings into appropriate .NET objects.

When the DefaultModelBinder is required to supply an instance of some custom .NET type, Reflection comes for the rescue. Using reflection, all the public properties exposed by the custom type receive values based on what the ValueProviderDictionary class provided. Do bear in mind that there’s some kind of recursion happening here and that’s how all properties and its sub-properties get set with values.

That was all the magic which MVC does behind the scenes and I hope this explanation helps somebody in clearing their doubts.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s