1

Hi I am trying to create one common ActionResult that can return redirectToAction with dynamic ActionName, ControllerName and object Parameter if any

public ActionResult partner()
        {
//Building my params
obj.parameters = string.Format("cId = {0}, aId= {1}", CustomerID, Session["LocationId"]);
 return RedirectToAction(obj.actionName, obj.controllerName, string.IsNullOrEmpty(obj.parameters) ? null : new { obj.parameters });
}

I am not sure if this is possible in MVC. Did any one had such requirement? is there any work around to achieve something like this.

HaBo
  • 13,999
  • 36
  • 114
  • 206
  • cant you get these values from RouteData – Rafay Feb 11 '13 at 20:07
  • no. these params are build based on business logic. in some instance there may be few parameters and some time none. With the above code it is rendering the url as /Controller/Action?parameters="cid=2,aid=3" – HaBo Feb 11 '13 at 20:21

1 Answers1

4

Here are a couple ideas that may help you out:

Option 1: Use an anonymously typed object containing the route values.

obj.parameters = new { cId = CustomerID, aId = Session["LocationId"] };
return RedirectToAction(obj.actionName, obj.controllerName, obj.parameters);

Option 2: Use a RouteValueDictionary as described in this answer to a similar question.

obj.parameters = new RouteValueDictionary();
obj.parameters['cId'] = CustomerID;
obj.parameters['aId'] = Session["LocationId"];
return RedirectToAction(obj.actionName, obj.controllerName, obj.parameters);
Community
  • 1
  • 1
ajk
  • 4,473
  • 2
  • 19
  • 24