Click here to Skip to main content
Click here to Skip to main content

MVC Contrib ControllerExtension - RedirectToAction

By , 14 Mar 2013
Rate this:
Please Sign up or sign in to vote.

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. 

using MvcContrib;
Afterwards, add a new class to your file, which will have the extended method which will accept AreaName as parameter.
///<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. 

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

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

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.
[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. 

License

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

About the Author

No Biography provided

Comments and Discussions

 
QuestionT4 MVC PinmemberVahid_N15-Mar-13 4:14 
GeneralPass the entity during navigation to specific area. PinmemberMember 875053614-Mar-13 20:18 
GeneralMy vote of 5 PinmemberMember 875053614-Mar-13 20:13 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 14 Mar 2013
Article Copyright 2013 by Arashdeep Kaur
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid