简体   繁体   中英

ASP.NET MVC RedirectToAction doesn't work after AJAX Post from view

I'm trying to reload my view from the controller, in order to display data stored in TempData after an AJAX POST call to my controller. The problem is that my call to the method RedirectToAction doesn't redirect anything.

Here's some code samples

View:

    $('#incidentTableBody').on('click', 'button.relayBack', function () {
            Post('/Incident/RelayBack', { incidentId: $(this).parent().parent().data('id'), ownerId: $(this).parent().parent().data('ownerid') }, function () {  });
        });

    function Post(action, data, callback) {
            $.ajax({
                url: action,
                type: "POST",
                data: data,
                //error: function (jqXHR, textStatus, errorThrown) {
                //    console.log(errorThrown);
                //},
                success: callback,
                cache: false
            });
        }

Controller:

    [Authorize]
    public ActionResult IncidentList()
    {
        ViewBag.Title = "IncidentList";
        return View();
    }

    [HttpPost]
    [Authorize]
    public ActionResult RelayBack(string incidentId, string ownerId)
    {
        bool success;
        try
        {
            new Guid(_incidentService.RelayBack(new Guid(incidentId), new Guid(ownerId)));
            success = true;
        }
        catch
        {
            success = false;
        }

        if (success)
        {
            TempData["Message"] = "Le ticket a bien été relancé par l'équipe support";
            TempData["Class"] = "alert-success";
        }
        else
        {
            TempData["Message"] = "Une erreur est survenue lors de la relance de l'incident";
            TempData["Class"] = "alert-warning";
        }

        return RedirectToAction("IncidentList"); // TODO : redirect doesn't work
    }

My AJAX call works and calls my controller, then all my controller method instructions but the last one (redirection) run correctly. The last one, RedirectToAction, actually calls IncidentList() that's supposed to return my view but nothing happens.

I tried to replace RedirectToAction with View, Redirect... nothing better happened. I tried to reload from my AJAX success callback function, but the behaviour wasn't what I expected, and I prefer to do it from my Controller.

I have already done the same (a RedirectToAction call to reload my page and display TempData stored data) in my app, but after a form POST submission and not an AJAX call, could it (AJAX) be the source of the problem?

so you want to redirect your page after post.

change your return method to Json .

return Json(new { redirectToUrl = Url.Action("action", "contoller") });

And OnSuccess Ajax

success: function (response) {
                window.location.href = response.redirectToUrl;
            }

That's it.

在Ajax调用中,使用“window.location = redirectUrl”以成功方法而不是回调方式重定向到特定页面。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM