JQuery在Controller上调用Action(但之后没有重定向)

时间:2022-12-01 22:39:15

I have a JQuery confirmation popup, with Yes, No buttons. The Yes button calls this function:

我有一个JQuery确认弹出窗口,带有Yes,No按钮。是按钮调用此功能:

   function doStuff() {
            $('#confirmPopup').dialog("close");
            var someKey = $("#someKey")[0].value;
            $.post("/MYController/MyAction", { someKey : someKey },
           function(responseText, textStatus, XMLHttpRequest) {

            });
            return false;
        }

This successfully calls my controller action(2 different attempts):

这成功调用了我的控制器动作(2次不同的尝试):

 public ActionResult MyAction(int someKey)
 {
     //do stuff
     return RedirectToAction("OtherAct", "OtherCont");
}



 public JavaScriptResult MyAction(int someKey)
     {
         //do stuff
         return JavaScript("Window.location.href='OtherCont/OtherAct';");
    }

In both cases the action gets executed, but re-direct to the other action does not occure. why?

在这两种情况下,操作都会执行,但不会发生重定向到其他操作。为什么?

4 个解决方案

#1


Because you cannot redirect from an action which is invoked asynchronously ($.post). Look here for alternative.

因为您无法从异步调用的操作($ .post)重定向。在这里寻找替代方案。

#2


OtherAct on OtherContController ... is it decorated with [AcceptVerbs(HttpVerbs.Post)]?

OtherContController上的OtherAct ...用[AcceptVerbs(HttpVerbs.Post)]装饰?

If so, there's the first of your problems. Your action that returns a JavaScriptResult will work. But if the action is decorated with an HttpVerbs.Post, then there is no Get action for that and the Ajax call is getting a 404 not found. Of course, since it is happening asynchronously, you'd have no way of knowing that. No error messages would be displayed.

如果是这样,那就是你的第一个问题。您返回JavaScriptResult的操作将起作用。但是如果操作是用HttpVerbs.Post修饰的,那么就没有Get操作,并且Ajax调用没有找到404。当然,由于它是异步发生的,你无法知道这一点。不会显示任何错误消息。

The second problem is easier. You said ...

第二个问题更容易。你说 ...

return JavaScript("Window.location.href='OtherCont/OtherAct';")

... when what you meant was ...

......当你的意思是......

return JavaScript("window.location.href='OtherCont/OtherAct';")

... or more properly ...

......或者更恰当......

return JavaScript("window.location.href='" + 
       Url.Action("OtherAct", "OtherCont") + "';");

That should get you there.

那应该会让你到那里。

#3


I have seen issues with Web Service redirect calls where they won't work this way.

我已经看到了Web Service重定向调用的问题,它们不会以这种方式工作。

Another approach is to send the url you want to redirect to back to the client, then use Javascript to do the redirect (Window.location.href = ....)

另一种方法是将要重定向的url发送回客户端,然后使用Javascript进行重定向(Window.location.href = ....)

Or, just do an HTML Submit instead of a web service call.

或者,只需执行HTML提交而不是Web服务调用。

#4


Because you're using ajax, which means a redirect from the action will be ignored.

因为您正在使用ajax,这意味着将忽略来自该操作的重定向。

A solution to have the redirect invoke could be to create another helper that uses a normal FORM POST and no ajax.. then the whole page will POST and the action will redirect.

重定向调用的解决方案可能是创建另一个使用普通FORM POST且没有ajax的帮助程序..然后整个页面将POST并且操作将重定向。

Here's part of my helper that does exactly that:

这是我的助手的一部分,它正是这样做的:

        string deleteLink = String.Format(@"<a onclick=""deleteRecord({0})"" href='#'>Delete</a><form id='deleteForm' method='post' action='" +
             routeRelativePath + "/" + actionPrefix + "Delete/" + model.ID +
            @"'></form>", model.ID);

.. more on the above.

..更多关于上述。

#1


Because you cannot redirect from an action which is invoked asynchronously ($.post). Look here for alternative.

因为您无法从异步调用的操作($ .post)重定向。在这里寻找替代方案。

#2


OtherAct on OtherContController ... is it decorated with [AcceptVerbs(HttpVerbs.Post)]?

OtherContController上的OtherAct ...用[AcceptVerbs(HttpVerbs.Post)]装饰?

If so, there's the first of your problems. Your action that returns a JavaScriptResult will work. But if the action is decorated with an HttpVerbs.Post, then there is no Get action for that and the Ajax call is getting a 404 not found. Of course, since it is happening asynchronously, you'd have no way of knowing that. No error messages would be displayed.

如果是这样,那就是你的第一个问题。您返回JavaScriptResult的操作将起作用。但是如果操作是用HttpVerbs.Post修饰的,那么就没有Get操作,并且Ajax调用没有找到404。当然,由于它是异步发生的,你无法知道这一点。不会显示任何错误消息。

The second problem is easier. You said ...

第二个问题更容易。你说 ...

return JavaScript("Window.location.href='OtherCont/OtherAct';")

... when what you meant was ...

......当你的意思是......

return JavaScript("window.location.href='OtherCont/OtherAct';")

... or more properly ...

......或者更恰当......

return JavaScript("window.location.href='" + 
       Url.Action("OtherAct", "OtherCont") + "';");

That should get you there.

那应该会让你到那里。

#3


I have seen issues with Web Service redirect calls where they won't work this way.

我已经看到了Web Service重定向调用的问题,它们不会以这种方式工作。

Another approach is to send the url you want to redirect to back to the client, then use Javascript to do the redirect (Window.location.href = ....)

另一种方法是将要重定向的url发送回客户端,然后使用Javascript进行重定向(Window.location.href = ....)

Or, just do an HTML Submit instead of a web service call.

或者,只需执行HTML提交而不是Web服务调用。

#4


Because you're using ajax, which means a redirect from the action will be ignored.

因为您正在使用ajax,这意味着将忽略来自该操作的重定向。

A solution to have the redirect invoke could be to create another helper that uses a normal FORM POST and no ajax.. then the whole page will POST and the action will redirect.

重定向调用的解决方案可能是创建另一个使用普通FORM POST且没有ajax的帮助程序..然后整个页面将POST并且操作将重定向。

Here's part of my helper that does exactly that:

这是我的助手的一部分,它正是这样做的:

        string deleteLink = String.Format(@"<a onclick=""deleteRecord({0})"" href='#'>Delete</a><form id='deleteForm' method='post' action='" +
             routeRelativePath + "/" + actionPrefix + "Delete/" + model.ID +
            @"'></form>", model.ID);

.. more on the above.

..更多关于上述。