C# ASP.NET MVC Return to Previous Page

后端 未结 4 1291
甜味超标
甜味超标 2020-12-13 03:08

I have a basic Edit method in my controller that redirects back to a top level listing (“Index”) when the edit succeeds. Standard behavior after MVC scaffolding.

I a

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-13 04:07

    Here is just another option you couold apply for ASP NET MVC.

    Normally you shoud use BaseController class for each Controller class.

    So inside of it's constructor method do following.

    public class BaseController : Controller
    {
            public BaseController()
            {
                // get the previous url and store it with view model
                ViewBag.PreviousUrl = System.Web.HttpContext.Current.Request.UrlReferrer;
            }
    }
    

    And now in ANY view you can do like

    
    

    Enjoy!

提交回复
热议问题