ActionLink back button
Asked Answered
V

3

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

Vicegerent answered 13/1, 2011 at 22:36 Comment(0)
P
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)
Pisgah answered 13/1, 2011 at 22:39 Comment(0)
D
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

Derma answered 14/1, 2011 at 7:18 Comment(0)
W
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.

Wife answered 25/8, 2011 at 3:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.