Monday, July 3, 2017

ASP.Net MVC Model Binding

 Model Binding

Model Binding

What is Model Binding?

Model Binding in ASP.Net MVC maps the data from HTTP request to action method parameters. These parameters may be simple types such as string, integer or they may be complex type. It is a great feature as it relieves developer from writing all the type casting and “HTML-Model mapping code”. The Model binder is capable of retrieving the data from HTML form variables, POSTed variables and files, query string parameters and the values added in the routes.

How model binding works?

When MVC receives HTTP request, it routes the request to a specific action method of a controller. It determines which action method is going to execute based on what is in the route data, then it maps values from the HTTP request to action method’s parameter(s). For ex. Consider the below URL-

Since the route template is looks like {controller}/{action}/{id?}, books/edit/2 routes to the Books controller and its Edit action method. It also accepts an optional parameter id. Action method code should look something like this-
public IActionResult Edit(int? id)
MVC will try to bind request data to the action parameters by name and use the DefaultModelBinder class which magically does all the type conversion and mapping of all value(s). MVC model binding binds the data from various parts of request and it does so in set of order. Below is a list of the data sources in the order that model binding looks through them:
  • Form values: These are form values that go in the HTTP request using the POST method. (including jQuery POST requests).
  • Route value: The set of route values provided by Routing.
  • Query strings: The query string part of the URL.

Since model binding asked for a key named id and there is nothing named id in the form values, it moved on to the route values looking for that key. In our example, it's a match. Binding happens, and the value is converted to the integer 2. The same request using Edit(string id) would convert to the string"2".

When a parameter is bound, model binding stops looking for values with that name and it moves on to bind the next parameter. If binding fails, MVC does not throw an error. You can query for model state errors by checking the ModelState.IsValid property.

Model Binding Attributes

MVC contains several attributes that you can use to direct its default model binding behavior to a different source. For example, you can specify whether binding is required for a property, or if it should never happen at all by using the [BindRequired] or [BindNever] attributes. Alternatively, you can override the default data source, and specify the model binder's data source. Below is a list of model binding attributes:
  • [BindRequired]: This attribute adds a model state error if binding cannot occur.
  • [BindNever]: Tells the model binder to never bind to this parameter.
  • [FromHeader], [FromQuery], [FromRoute], [FromForm]: Use these to specify the exact binding source you want to apply.
  • [FromServices]: This attribute uses dependency injection to bind parameters from services.
  • [FromBody]: Use the configured formatters to bind data from the request body. The formatter is selected based on content type of the request.
  • [ModelBinder]: Used to override the default model binder, binding source and name.
Attributes are very helpful tools when you need to override the default behavior of model binding.

In this article, we have only discussed about what is ASP.NET Model binding and saw a basic introduction to the Model Binder. The model binding process can be customized by implementing the custom model binder and value providers.

No comments:

Post a Comment

If You Enjoyed This Post Please Take 5 Seconds To Share It.

^ Scroll to Top hgpromo