RedirectToAction with parameter

921,933

Solution 1

You can pass the id as part of the routeValues parameter of the RedirectToAction() method.

return RedirectToAction("Action", new { id = 99 });

This will cause a redirect to Site/Controller/Action/99. No need for temp or any kind of view data.

Solution 2

Kurt's answer should be right, from my research, but when I tried it I had to do this to get it to actually work for me:

return RedirectToAction( "Main", new RouteValueDictionary( 
    new { controller = controllerName, action = "Main", Id = Id } ) );

If I didn't specify the controller and the action in the RouteValueDictionary it didn't work.

Also when coded like this, the first parameter (Action) seems to be ignored. So if you just specify the controller in the Dict, and expect the first parameter to specify the Action, it does not work either.

If you are coming along later, try Kurt's answer first, and if you still have issues try this one.

Solution 3

RedirectToAction with parameter:

return RedirectToAction("Action","controller", new {@id=id});

Solution 4

It is also worth noting that you can pass through more than 1 parameter. id will be used to make up part of the URL and any others will be passed through as parameters after a ? in the url and will be UrlEncoded as default.

e.g.

return RedirectToAction("ACTION", "CONTROLLER", new {
           id = 99, otherParam = "Something", anotherParam = "OtherStuff" 
       });

So the url would be:

    /CONTROLLER/ACTION/99?otherParam=Something&anotherParam=OtherStuff

These can then be referenced by your controller:

public ActionResult ACTION(string id, string otherParam, string anotherParam) {
   // Your code
          }

Solution 5

//How to use RedirectToAction in MVC

return RedirectToAction("actionName", "ControllerName", routevalue);

example

return RedirectToAction("Index", "Home", new { id = 2});
Share:
921,933
Eric Brown - Cal
Author by

Eric Brown - Cal

I'm a Senior Software Developer working for Buchanan & Edwards a company that has a contract to service the Job Corps Data Center.

Updated on July 15, 2022

Comments

  • Eric Brown - Cal
    Eric Brown - Cal almost 2 years

    I have an action I call from an anchor thusly, Site/Controller/Action/ID where ID is an int.

    Later on I need to redirect to this same Action from a Controller.

    Is there a clever way to do this? Currently I'm stashing ID in tempdata, but when you hit f5 to refresh the page again after going back, the tempdata is gone and the page crashes.

  • Diego
    Diego almost 13 years
    Is there anyway of doing that or something similar but also specifying the controller? (I need to do that from one controller to an action of other controller).
  • Kurt Schindler
    Kurt Schindler almost 13 years
    @Diego: yes, there's are a couple overloads for that. In my example it would be RedirectToAction("Action", "Controller", new{id=99}) msdn.microsoft.com/en-us/library/dd470154.aspx
  • Jeremy A. West
    Jeremy A. West about 10 years
    VB - Return RedirectToAction("Action", "Controller", New With {.id = 99})
  • mellis481
    mellis481 over 9 years
    Great answer. Also, I believe the controller parameter is optional if the redirect action is in the same controller as the action you're redirecting from.
  • Eric Brown - Cal
    Eric Brown - Cal over 9 years
    it's supposed to be, but when I did it that way, it didn't work, I had to explicitly add the controller... that was literally in my first days of MVC, if I had to guess now I'd say I had some kind of routing setup issue to look into.
  • mellis481
    mellis481 over 9 years
    Weird. Works for me in MVC4.
  • Eric Brown - Cal
    Eric Brown - Cal over 9 years
    This happened back in MVC1 days, and it worked fine for others, that's why I suspect a configuration issue, looking back.
  • Zameer Ansari
    Zameer Ansari almost 9 years
    Does it work? I believe this is incorrect syntax. Correct me if I'm wrong.
  • Worthy7
    Worthy7 almost 8 years
    Is there a way to do the same thing but not have the url update like so?
  • Edward
    Edward about 7 years
    @KurtSchindler Anyway to update your answer to include your comment example of a redirect to different controller as well? To help those who might skip over reading comments.
  • Edward
    Edward about 7 years
    @EricBrown-Cal Given even your own submission in your comments don't you think Kurt's answer is better suited for those seeking an answer to the same issue you had in your beginning days?
  • Edward
    Edward about 7 years
    @pb2q new {id = id} works just as well because the framework knows which variable you are referring to.
  • Eric Brown - Cal
    Eric Brown - Cal about 7 years
    Except for the fact it didn't work for me to solve the problem in question, yes. Given that, explaining both those facts seemed the most useful answer. SO doesn't allow dual answers, so I upvoted Kirk's and marked the one that actually solved my issue.
  • Robert
    Robert over 6 years
    I have a similar need and am doing this. Works, but it places the data in the query string in the browser URL. For me it's a user id and I can't have that info appear there. Any way to do it under the covers? Think maybe I'll post this separately.
  • Robert
    Robert over 6 years
    I am doing this and it works. But the data appears in the query string in the browser URL. My parameter is a user ID so I can't have it display there. Any way to do this under the covers? Maybe I'll post this separately.
  • wpqs
    wpqs over 5 years
    For information about passing objects as json strings, see stackoverflow.com/questions/7597863/…
  • jakobinn
    jakobinn about 5 years
    It's worth noting that the name of the ID parameter matters in terms of how the parameter appears in the URL, which is based on how the RouteConfig file is and how routing mechanisms are. For instance, new { id = 100 } can produce the URL "/100", while new {groupId = 100} might be interpreted as a query string (as was mentioned above), resulting in the URL "?groupId=100".
  • orangecaterpillar
    orangecaterpillar about 4 years
    RedirectToAction("Action", "Controller", new { id = id}); was just what I needed in my Core 3.1 app.
  • user2693802
    user2693802 over 3 years
    This works for me also the option from pb2q works. I suppose depends how you have set the routhing in your project one or another option may not work. This solved my issue
  • Eric Brown - Cal
    Eric Brown - Cal about 3 years
    Given it's age, i moved the answer to Kurt's answer.
  • Eric Brown - Cal
    Eric Brown - Cal almost 3 years
    I don't know why the new is sometimes needed, but it's good to have both here :)