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.
public static class MyControllerExtensions
{
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.