Click here to Skip to main content
15,891,184 members
Articles / Programming Languages / C#
Tip/Trick

MVC Contrib ControllerExtension - RedirectToAction

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
14 Mar 2013CPOL2 min read 20.3K   4   3
Passing parameters in RedirectToAction method across portable areas

Introduction

This article contains a solution to an issue in MVCContrib controller extension RedirectToAction method, which is used to pass complex parameters between action methods. MVCContrib provides an extension to MVC base method RedirectToAction, in which you can pass complex objects as parameters. But the issue which I faced while using this method was to use it across portable areas. Within the same solution it can be used, but to use it across a portable area, we need to write our own overload for this. 

Background  

The RedirectToAction method is used to redirect the control from one action method to other. While redirecting we can pass primitive parameters as route values. But if we need to pass complex parameters as object, then we have an extension method provided by MVC Contrib library (MVC Contrib is an open source library which provide extension over built in .net libraries). This extension method works fine while working in a single solution or area, but if we need to redirect to an action that is in different portable area then we are not able to do so. To solve this problem I have written an extension to MVC contrib extension method RedirectToAction(), which can take area name as parameter and can pass parameter to different actions around areas.

I have also raised this issue on MVC Contrib site, which can be viewed here: http://mvccontrib.codeplex.com/workitem/720 

Using the code 

To use MVC contrib extension method we first need to download latest version of MVC contrib DLL from:  http://mvccontrib.codeplex.com 

After downloading the MVC Contrib DLL, add a reference of this DLL to your project. Then refer to this with the using clause in your class. 

C#
using MvcContrib;
Afterwards, add a new class to your file, which will have the extended method which will accept AreaName as parameter.
C#
///<summary>
/// Static class containing extension methods for controllers
///</summary>
public static class MyControllerExtensions
{
/// <summary>
/// Redirects to an action on the same controller using expression-based syntax
/// </summary>
/// <typeparam name="T">The type of the controller on which to call the action</typeparam>
/// <param name="controller">The instance of the controller of type
///   <typeparamref name="T"/> which provides access to this method</param>
/// <param name="action">An expression which identifies the action to redirect
///   to on the controller of type <typeparamref name="T"/></param>
/// <param name="area">An expression which identifies the route to redirect to on the controller of type</param>
/// <returns>A <see cref="RedirectToRouteResult"/> pointing to the action
///    specified by the <paramref name="action"/> expression</returns> 
public static RedirectToRouteResult RedirectToAction<T>(this T controller, 
       Expression<Action<T>> action, string area) where T : Controller
{
    var routeValue = ((Controller)controller).RedirectToAction(action);
    if (!string.IsNullOrEmpty(area))
    {
        routeValue.RouteValues["Controller"] = area + "/" + routeValue.RouteValues["Controller"];
    }
    else
    {
        routeValue.RouteValues["Controller"] = routeValue.RouteValues["Controller"];
    }
    return routeValue;
}
} 

After adding this class, we can use this extension method from anywhere within the same namespace. To use this method firstly we have to add an attribute to the action method from where we want to redirect to another action method. By passing the complex parameter, add the [PassParameterDuringRedirect] attribute. 

C#
[PassParametersDuringRedirect]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Configuration(ConfigurationViewModel model)
{ 

Now at the end of the action method, write the RedirectToAction method. 

C#
return MyControllerExtensions.RedirectToAction(new YourControllerName(), c => c.ActionMethodName(Parameter), "AreaName");
At the end, we also need to add the [PassParameterDuringRedirect] attribute to the controller class to which our RedirectToAction method is redirecting.
C#
[PassParametersDuringRedirect]
public class LiabilityPlanController : HomeBaseController
{ 

Conclusion 

This solution solves the problem of passing complex parameters across portable areas using MVCContrib. Please feel free to reach out to me if you have any doubts or suggestions. 

This article was originally posted at http://mvccontrib.codeplex.com/workitem/720

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionT4 MVC Pin
Vahid_N15-Mar-13 4:14
Vahid_N15-Mar-13 4:14 
GeneralPass the entity during navigation to specific area. Pin
Abhay@Accenture14-Mar-13 20:18
Abhay@Accenture14-Mar-13 20:18 
GeneralMy vote of 5 Pin
Abhay@Accenture14-Mar-13 20:13
Abhay@Accenture14-Mar-13 20:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.