开发者

ActionLink back button

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")%> an开发者_如何学JAVAd this is an example of the paging links: <%= Html.RouteLink("<< First Page", "NewsPaging", new { query = ViewData["query"], page = 0 })%>

Thanks


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)


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜