How can I make MVC POST return me to the previous page?

I have the following action, which is called from the list of records screen.

[HttpPost] //[Authorize(Roles = "admin")] public ActionResult Edit(EditViewModel itemView) { 

As soon as the action is completed, I would like to return to the page from which the action was called. However, I do not want to refresh this page. I just want to return to the filled screen using something similar to the Previous button in the browser. Right now, when I click save, my action does the following, which is not what I want:

 return RedirectToAction("Index"); 

Is there a way to redirect to the previous page using MVC3?

Something like the Stackoverflow function after clicking the "Edit" button to edit the response where it returns to the message.

+4
source share
3 answers

Based on the code you provided, it looks like you have a paginated screen with the ability to click on the edit on each line. This is how I solved this problem in the past.

On the Index page, when the page loads, whether from the main index or the swap method, add the following:

 Session["CurrentUrl"] = Request.Url.ToString(); 

So, at the end of the POST method for your edit page, do:

 return Session["CurrentUrl"] == null ? Index() : Redirect(Session["CurrentUrl"]); 
+1
source

Instead of redirecting

 [HttpGet] public ActionResult Index() { /// preform any processing necessary for your index page on GET return View("Index"); } [HttpPost] public ActionResuit Edit(EditViewModel itemView) { if (ModelState.IsValid) { /// do whatever you want with your model... } // return the contents as they'd be rendered by the Index action return Index(); } 

Note that using this method, the URL in the browser will still display the Edit URL (e.g. /area_name/edit ), but you can fix this:

  • Using redirects (which you said you don't want to do)
  • Using JavaScript to update the URL or using history.back() as suggested by @AlanStephens
  • Perhaps other methods that do not immediately come to mind.

However, I would ask if this is really the best approach. Usually, users expect different URLs to do different things.

Or, if I understand you correctly and the editing action is called from the Index page,

 [HttpGet] public ActionResult Index() { return View(); } [HttpPost] /// from a form on Index public ActionResult Index(EditViewModel model) { if (ModelState.IsValid) { //// } return View(); } 

and just remove /Edit from the game completely. Again, I do not really like this approach.

+2
source

What you described is easily achieved using ajax calls. This way, you perform any action that you like, and then (with a successful answer) you can easily jump from the current page using javascript.

If you send a message to the page and in return you return the same view that you get with the GET request (index page), then some users can press F5 to reload this index page and get a warning in the browser, which actually says it will send the POST request again. This is rather confusing for users and not very convenient for users (not to mention the numerous problems associated with its capacity).

Although you don't like the redirect approach, due to the additional answer, I think I should say that in MVC this is the right way to do this, assuming you don't want to use ajax calls.

0
source

Source: https://habr.com/ru/post/1379348/


All Articles