Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 501
Alex Avatar asked Dec 20 '17 10:12

Alex


People also ask

Is RedirectToAction a post or a get?

When you call RedirectToAction within a controller, it automatically redirects using an HTTP GET.

What is difference between redirect and RedirectToAction?

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.

How to redirect to Action in AJAX success?

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


1 Answers

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.

like image 120
Tonmoy Saha Avatar answered Oct 12 '22 11:10

Tonmoy Saha