HaBo
HaBo

Reputation: 14297

Build Dynamic parameter objects in ASP.Net MVC

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.

Upvotes: 1

Views: 1966

Answers (1)

ajk
ajk

Reputation: 4603

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);

Upvotes: 4

Related Questions