Invoke Action With Model Binders in MVC

In this article, I'll share my thoughts about invoking an action with Custom model binders and how to register your own model binders in MVC.

So let's start the journey.

An Excerpt about model binding is given below:

Model binding is the process of creating .NET objects using the data sent by the browser in an HTTP request. We have been relying on the model binding process each time define an action method that takes a parameter; the parameter objects are created by model binding.

Model binding is ASP.NET MVC's mechanism for mapping HTTP request data directly into action method parameters and custom .Net objects.

For example, when we receive a request for a URL such as "/Home/ RsvpForm/23", the MVC Framework must map the details of the request in such a way that it can pass appropriate values or objects as parameters to our action method.

The action invoker, the component that invokes action methods, is responsible for obtaining values for parameters before it can invoke the action method.

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 each parameter type.

  1. // GET: /Register/Details/5  
  2. public ActionResult Details(int Id)  
  3. {  
  4.     return View("Employee Details");  
  5. }
For example, as shown in the code segment ablove, the action invoker would find that the action method had one int parameter, so it would locate the binder responsible for binding int values and call its BindModel method. If there is no binder that will operate on int values then the default model binder is used.

Let's move ahead and create a Model binder that meets our desire.

I've created a model binder "EmpRegistrationModelBinder" that implements the IModelBinder interface. The following is the code snippet:

MVC1.jpg

After implementing EmpRegistrationModelBinder it's time to register the custom binder in Global.asax so that could be applicable at the application level.

Here is the code snippet to register a binder into Global.asax:

MVC2.jpg

The line "ModelBinders.Binders.Add(typeof(EmpRegistration), new EmpRegistrationModelBinder());

Says that whenever a parameter to an action method is EmpRegistration, use this custom model binder. Here is my action method:

MVC3.jpg

Note: if you want to do something similar for a different action then you can use the custom binder as a parameter for the other action. Since I've registered for the two action methods depicted below:

MVC4.jpg

Now I run my application and perform the desired action to meet the results. Press F5.

Step 1

First, it jumps into Global.asax and registers the binder that would be applicable at the application level.

Press F5. It gives the output as shown below:

MVC5.jpg

Now I click on the "Details" button and see the behavior:

Step 2

It jumps into the EmpRegistrationModelBinder class and executes the business logic. Again I press F5.

Now it reaches into the RegisterController's class Details method that has the parameter EmpRegistration.

Note that the emp object being shown in the following image has all the details of the user Abhinav that we have retrieved through the Model Binder class..

MVC6.jpg

Press F5 and see the output:

MVC9.jpg

Hope it'll help you to understand the facts "how to register Custom Model Binders and Invoking action method".

The sample application has been attached as a reference.

Keep coding and Stay Happy.


Similar Articles