ActionLink back button

前端 未结 3 2091
旧巷少年郎
旧巷少年郎 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 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:

    < Back
    

    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.

提交回复
热议问题