3

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!

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks slfan, the various options are good to know. I was hoping MVC would keep a hidden object somewhere with my route parameters that I could reference.
I added a code sample to access the route parameters. Hope this helps.
2

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.

1 Comment

Thank you Oenning. This seems like a good method, I'm going to try and implement it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.