从MVAX Post查看后,ASP.NET MVC RedirectToAction不起作用

时间:2022-09-21 03:20:00

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.

我正在尝试从控制器重新加载我的视图,以便在对我的控制器进行AJAX POST调用后显示存储在TempData中的数据。问题是我对RedirectToAction方法的调用不会重定向任何东西。

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.

我的AJAX调用工作并调用我的控制器,然后我的所有控制器方法指令,但最后一个(重定向)正确运行。最后一个,RedirectToAction,实际上调用了IncidentList(),它应该返回我的视图,但没有任何反应。

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.

我试图用View,Redirect替换RedirectToAction ......没有更好的事情发生。我试图从我的AJAX成功回调函数重新加载,但行为不是我所期望的,我更喜欢从我的控制器做。

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?

我已经在我的应用程序中完成了相同的操作(RedirectToAction调用以重新加载我的页面并显示TempData存储的数据),但是在表单POST提交而不是AJAX调用之后,它(AJAX)是否可能是问题的根源?

2 个解决方案

#1


0  

so you want to redirect your page after post.

所以你想在发布后重定向你的页面。

change your return method to Json .

将您的返回方法更改为Json。

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

And OnSuccess Ajax

和OnSuccess Ajax

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

That's it.

#2


0  

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

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

#1


0  

so you want to redirect your page after post.

所以你想在发布后重定向你的页面。

change your return method to Json .

将您的返回方法更改为Json。

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

And OnSuccess Ajax

和OnSuccess Ajax

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

That's it.

#2


0  

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

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