ActionLink back button

前端 未结 3 2090
旧巷少年郎
旧巷少年郎 2020-12-21 18:42

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

相关标签:
3条回答
  • 2020-12-21 18:58

    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)
    
    0 讨论(0)
  • 2020-12-21 19:05

    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

    0 讨论(0)
  • 2020-12-21 19:06

    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 讨论(0)
提交回复
热议问题