12
votes

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?

2
The whole point of ajax is to stay on the same page. Ajax calls never redirect. If you want to redirect, do NOT use ajax.user3559349

2 Answers

21
votes

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.

0
votes

In Ajax call use "window.location = redirectUrl" to redirect to a specific page in success method instead of the callback.