Model Binding in ASP.NET MVC 5

model binding in mvc 5

In this MVC tutorial, we will learn about model binding in MVC framework.

How model binding works in MVC

To understand the model binding in MVC, first, let’s see how you can get the HTTP request values in the action method using traditional ASP.NET style. The following figure shows how you can get the values from HttpGET and HttpPOST request by using the Request object directly in the action method.

Accessing Request Data

As you can see in the above figure, we use the Request.QueryString and Request (Request.Form) object to get the value from HttpGet and HttpPOST request. Accessing request values using the Request object is a cumbersome and time-wasting activity.

With model binding, the MVC framework converts the Http request values (from the query string or form collection) to action method parameters. These parameters can be of primitive type or complex type.

Binding to Primitive type

HttpGET request embeds data into a query string. MVC framework automatically converts a query string to the action method parameters. For example, the query string “id” in the following GET request would automatically be mapped to the id parameter of the Edit() action method.

This binding is case insensitive. So “id” parameter can be “ID” or “Id”.

You can also have multiple parameters in the action method with different data types. Query string values will be converted into parameters based on matching the name.

For example, http://localhost/Student/Edit?id=1&name=John would map to id and name parameter of the following Edit action method.

Example: Convert QueryString to Action Method Parameters

Model binding also works on complex types. Model binding in MVC framework automatically converts form field data of HttpPOST request to the properties of a complex type parameter of an action method. Consider the following model classes.

Example: Model classes in C#
Example: Action Method with Class Type Parameter
Model Binding

So thus, it automatically binds form fields to the complex type parameter of an action method.


You can also include FormCollection type parameter in the action method instead of complex type, to retrieve all the values from view form fields as shown below.

Model Binding

Bind Attribute in MVC

ASP.NET MVC framework also enables you to specify which properties of a model class you want to bind. The [Bind] attribute will let you specify the exact properties a model binder should include or exclude in binding.

In the following example, the Edit action method will only bind the StudentId and StudentName property of a Student model.

Example: Binding Parameters

You can also use Exclude properties as below.

Example: Exclude Properties in Binding

The Bind attribute will improve the performance by only bind properties which you needed.

Inside Model Binding

As you have seen that Model binding automatically converts request values into a primitive or complex type object. Model binding is a two-step process. First, it collects values from the incoming Http request and second, populates primitive type or a complex type with these values.

Value providers are responsible for collecting values from request and Model Binders are responsible for populating values.

Default value provider collection evaluates values from the following sources:

  • Previously bound action parameters, when the action is a child actor.
  • Form fields (Request.Form).
  • The property values in the JSON Request body (Request.InputStream), but only when the request is an AJAX request.
  • Route data (RouteData.Values).
  • Querystring parameters (Request.QueryString).
  • Posted files (Request.Files).

MVC includes DefaultModelBinder class which effectively binds most of the model types.

Visit MSDN for detailed information on the Model binding.


I hope you liked this article about how to create a model binding in MVC and bind attributes in MVC. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.


Please enter your comment!
Please enter your name here