Ben Finkel
Ben Finkel

Reputation: 4803

Maintain url parameters while navigating

My Index action for a controller takes a page parameter like so:

/Organizations/Index?page=5

Which is used to limit the number of objects displayed. If they choose to "edit" one of those objects after they are finished I would like to return with the same values as before they began editing (e.g. be on the same "page" of the list).

My edit url ends up looking like this:

/Organizations/Edit/487

How do I persist the original page value?

Thanks!

Upvotes: 3

Views: 2690

Answers (2)

slfan
slfan

Reputation: 9129

To persist data between calls you can use

  • Session state,
  • a hidden field,
  • render it into the links as a query string,
  • use a cookie or
  • TempData (which is Session state kept only for the next call).

If you want to access the route data, you can use the controller context:

ControllerContext.RouteData.Values["action"];

"action" is the name of the route parameter.

Upvotes: 5

goenning
goenning

Reputation: 6654

I found two options:

1) Use a Source GET parameter all the time. Like this: /Organizations/Edit/487?Source=/Organizations/Index?page=5

The problem here is that the URL gets ugly.

2) You can do what slfan said using hdden fields (I don't like to use Session for this). First time you enter the edit view, catch the HttpContext.Current.Request.UrlReferrer property and save it to a hidden field. This way, if you do lots of POSTs you won't lose the original UrlReferrer, which is the url with the page parameter.

Upvotes: 2

Related Questions