2

I have an Index method that does double duty as showing a list of posts and a queried list of posts and can also have pages so you get urls like /News/Page/1 or /News?query=test

When a user clicks through to a post at say News/Details/1 they get a simple ActionLink that takes them back to the list. BUT I want this link to take them back to the actual page they were on in terms of the paging or the query. How could I do this? I don't want to use the JavaScript history method. Here is my current ActionLink: <%=Html.ActionLink("<< Back to News List", "Index")%> and this is an example of the paging links: <%= Html.RouteLink("<< First Page", "NewsPaging", new { query = ViewData["query"], page = 0 })%>

Thanks

Cameron
  • 27,963
  • 100
  • 281
  • 483

3 Answers3

10

It would probably be easier to just use javascript to send them back to the last page in their history (without creating a link to the specific page).

history.go(-1)
Brian Ball
  • 12,268
  • 3
  • 40
  • 51
4

I will go for a ActionLink like

<%=Html.ActionLink("<< Back to News List", "Index")%>

because user can access News/Details/1 directly and if then you have a link that uses history.go(-1) or history.back() function it will not redirect it to the Index action

Fraz Sundal
  • 10,288
  • 22
  • 81
  • 132
0

Another way to do this is to create the link in the controller and assign it to a property of the model or a simple ViewBag variable (up to you). For this example, I'm just going to use ViewBag.

So in your example, going from /News/Details/1 back to /News/Page/1?some-querystring, you can do the following:

In your controller (assuming '1' is the 'id'):

ViewBag.BackButton = String.Format("/News/Page/{0}{1}", id.ToString(), Request.Url.Query);

In your view:

<a href="@ViewBag.BackButton">&lt; Back</a>

One draw back to this is that you must know what the previous page was/could be. In your case, you want your back button to go back to your list, so this technique should be fine.

mnsr
  • 12,337
  • 4
  • 53
  • 79