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

后端 未结 3 1087
遥遥无期
遥遥无期 2021-01-14 00:41

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

    [HttpPost]
    //[Authorize(Roles = \"admin\")]
    public ActionResul         


        
相关标签:
3条回答
  • 2021-01-14 01:36

    What you described is easily achieved using ajax calls. That way you perform whatever action you like and afterwards (on successful response), you can easily navigate from the current page, using javascript.

    If you POST to a page and in response you return the same view you receive with a GET request (index page), then some users might hit F5 to reload that index page and get a warning in the browser, which actually says it will send the POST request again. This is pretty confusing for users and not really user friendly (not to mention numerous concerns related to idem-potency of it).

    Although you don't like the redirect approach, because of the additional response, I guess, I should say that, in MVC, this is the correct way to do it, assuming you don't want to use ajax calls.

    0 讨论(0)
  • 2021-01-14 01:37

    Based off the code you've given, it looks like you've got a paginated screen, with the ability to click edit on each row. Here's how I've solved this problem in the past.

    On the Index page, when the page loads, whether it be from the main index or a paging method, add the following:

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

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

    return Session["CurrentUrl"] == null ?
        Index() :
        Redirect(Session["CurrentUrl"]);
    
    0 讨论(0)
  • 2021-01-14 01:41

    Instead of a redirection,

    [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 with this method the URL in the browser will still display the Edit url (like /area_name/edit), but you can fix that by:

    1. Using a redirect (which you've said you don't want to do)
    2. Using JavaScript to update the URL, or use history.back() as @AlanStephens suggested
    3. Probably other methods that don't immediately come to mind.

    However, I'd question whether this is really the best approach. Typically, users expect different URLs to do different things.

    Or, if I understand you correctly and the edit action is being 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 take the /Edit out of play entirely. Again, I don't really care for this approach.

    0 讨论(0)
提交回复
热议问题