views:

806

answers:

3

Heading

I want to pass some variables from one page to another but I don't want them to appear in the URL. When I use a form to post the an ActionResult it works fine. However, when I do this

return RedirectToAction("Trackers",new{page = 1, orderby=desc});

I get the URL:

http://examplesite.com/Trackers?page=1&orderby=desc

Is there any way I can get the following URL instead but still pass the page and orderby variables "behind the scenes"?

http://examplesite.com/Trackers

Thanks TheLorax

A: 

When you use RedirectToAction method the browser will recieve an http redirect response from the server and then will do a GET request to the new URL.
I think there is no way to make the browser make a POST request when redirecting.

Also it is better to pass these variables through the query string in order for the user to be able to bookmark the page.

Marwan Aouida
+1  A: 

You could pass a Model object containing the page number and the orderby instead of an anonymous object, and have your view inherit the model class of the object you are passing. Your model object with the page number and orderby then becomes available to the view, without employing a query string

This is not, however, the preferred method of doing this. Your model objects should be reserved for their intended purpose, which is passing actual data to your view.

Robert Harvey
+3  A: 

I'm not sure it's a good idea to do what you're suggesting, however, you might be able to use the TempData (same place as the ViewData) to save your information before the redirect. I believe that after your next request, anything in the temp data is cleared out.

Hugoware
Hi HBoss, thanks for your response it was exactly what I wanted. The variable I wanted to pass was something I didn't want the user to see and is the reason I wanted to pass it invisibly. Your answer was exactly what I was looking for. I didn't even know anything about TempData before but now I do, so Thank You again.
The_Lorax