Simeon Wislang
Simeon Wislang

Reputation: 461

ASP.NET MVC parsing variables wihout URL query string

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

Upvotes: 2

Views: 1560

Answers (3)

hugoware
hugoware

Reputation: 36397

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.

Upvotes: 4

Robert Harvey
Robert Harvey

Reputation: 180787

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.

Upvotes: 2

user434917
user434917

Reputation:

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.

Upvotes: 1

Related Questions