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.

2
  • cant you get these values from RouteData Commented Feb 11, 2013 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" Commented Feb 11, 2013 at 20:21

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.