tags:

views:

3094

answers:

3

I have an action i call from a anchor thusly, Site/Controller/Action/ID where id = 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.

Thanks,

E-

A: 

I would put the ID in ViewData , and reference it in a hidden control on the page. That way you can round-trip the value.

If you are using a strongly-typed view, include the id in your View Model instead.

Robert Harvey
+15  A: 

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.

Kurt Schindler
+1  A: 

Kurts 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 RoutValueDictionary 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 etiher.

Thanks for the help!

Eric-

Eric Brown - Cal