RedirectToAction not changing URL or navigating to Index view

后端 未结 8 856
长情又很酷
长情又很酷 2020-12-01 07:49

I tried using a RedirectToAction after I have done a post to the controller and saved but the URL does not change and the redirect does not seem to work. I need

相关标签:
8条回答
  • 2020-12-01 08:30

    The code you show is correct. My wild guess is that you're not doing a standard POST (e.g., redirects don't work with an AJAX post).

    The browser will ignore a redirect response to an AJAX POST. It's up to you to redirect in script if you need to redirect when an AJAX call returns a redirect response.

    0 讨论(0)
  • 2020-12-01 08:32

    You need to add "return false;" to end of your onclick javascript event. For example:

    Razor Code

    @using (Html.BeginForm())
    {
        @Html.HiddenFor(model => model.ID) 
        @Html.ActionLink("Save", "SaveAction", "MainController", null, new { @class = "saveButton", onclick = "return false;" })
    }
    

    JQuery Code

    $(document).ready(function () {
            $('.saveButton').click(function () {
                $(this).closest('form')[0].submit();
            });
        });
    

    C#

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult SaveAction(SaveViewModel model)
    {
        return RedirectToAction("Index");
    }
    
    0 讨论(0)
提交回复
热议问题