Virtual Classes Logo          Virtual Classes
    Read Anytime,Anywhere

Model Binding

Model binding is the process of creating .NET objects using the data sent by the browser in an HTTP request.
Model binders are defined by the IModelBinder interface.
The IModelBinder Interface
namespace System.Web.Mvc
{
      public interface IModelBinder {
      object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext);
      }
}
There can be multiple model binders in an MVC application, and each binder can be responsible for binding one or more model types.
When the action invoker needs to call an action method, it looks at the parameters that the method defines and finds the responsible model binder for the type of each one.
You can define custom model binders,but mostly developer rely on the built-in model binder class that is DefaultModelBinder. This is the default binder that is used by the action invoker when it does not find a custom binder to bind the type.
This model binder searches four locations to bind data matching the name of the parameter being bound.
1-Request.Form- Values provided by the user in HTML form elements.
2-RouteData.Values- The values obtained using the application routes.
3-Request.QueryString- Data included in the query string portion of the request URL.
4-Request.Files- Files that have been uploaded as part of the request.

The model binding process is performed automatically when an action method defines parameters, but we can take direct control of the process if we want to.
This gives us more explicit control over how model objects are instantiated, where data values are obtained from, and how data parsing errors are handled.