Wednesday, April 16, 2014

Invoke action with Model Binders in MVC

Invoke action with Model Binders in MVC

In this article I’ll share my thoughts about invoking 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 we have defined 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.

e.g.

When we receive a request for a URL such as /Home/ RsvpForm/23, the MVC Framework has to 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.

// GET: /Register/Details/5

public ActionResult Details(int Id)

{

return View("Employee Details");

}

For an e.g. as shown 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 which meets our desire.

I’ve created a model binder EmpRegistrationModelBinder which implements the IModelBinder interface .Here is the code snippet as showing below:

clip_image002

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

Here is the code snippet to register binder into Global.asax as depict below:

clip_image004

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

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

clip_image006

Note: if you want to perform similar set of functionality for different action than you can use the custom binder as parameter for different Action.as I’ve registered for two action method depict below:

clip_image008

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

Step1.

First it jumps into Global.asax and register the binder which would be applicable at application level:

Press F5.It gives the output as shown below:

clip_image010

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

Step2: It jumps into the EmpRegistrationModelBinder class and execute the business logic. Again I press F5.

Now it reaches into RegisterController’s class Details method which has parameter of EmpRegistration.

If you notice that the emp object being shown in the below image has all the details of user Abhinav which we have retrieved through the Model Binder class..

clip_image012

Press F5 and see the output:

clip_image014

Hope it’ll help you to understand the facts “how to register Custom Model Binders and Invoking action method”.

Sample application has been attached as a reference at this link.

Invoke Action With Model Binders in MVC

Keep coding and Stay Happy Smile

0 comments :

Post a Comment