asp.net mvc ajax post - redirecttoaction无效

时间:2022-09-27 03:18:39

I used the following code in one of my controller;

我在我的一个控制器中使用了以下代码;

            if (Request.IsAjaxRequest()) {
            return RedirectToAction("PreviewAndSendEmail");
        }

I debugged it and it comes to return line and but redirect didn't occur. Is it possible to do that inside Ajax.BeginForm ? Here is the razor code;

我调试它,它返回到行,但没有发生重定向。是否可以在Ajax.BeginForm中执行此操作?这是剃刀代码;

    using(Ajax.BeginForm( new AjaxOptions { LoadingElementId = "loading" })) { 

    <b>Choose E-mail Template : </b>@Html.DropDownList("emailtemps")<br /><br />

    <input type="submit" value="Preview & Send" />

    <span id="loading" style="display: none;">
        <img title="loading..." alt="load" src="@Url.Content("~/Content/App_Icons/gifs/loading.gif")"
    </span>

}

2 个解决方案

#1


15  

You can't redirect in an AJAX action from the server. If you want your browser to redirect in an AJAX action you need to do it from javascript. Obviously using AJAX to redirect is absolutely useless. If you intend to redirect use a normal Html.Begin form and don't bother with AJAX.

您无法从服务器重定向AJAX操作。如果您希望浏览器在AJAX操作中重定向,则需要通过javascript进行操作。显然使用AJAX重定向绝对没用。如果您打算重定向使用普通的Html.Begin表单而不打扰AJAX。

#2


1  

Why don't you call the PreviewAndSendMailEmail directly? Since you don't need the redirection itself (change in url,etc), there is no need to use it.

你为什么不直接调用PreviewAndSendMailEmail?由于您不需要重定向(更改URL等),因此无需使用它。

#1


15  

You can't redirect in an AJAX action from the server. If you want your browser to redirect in an AJAX action you need to do it from javascript. Obviously using AJAX to redirect is absolutely useless. If you intend to redirect use a normal Html.Begin form and don't bother with AJAX.

您无法从服务器重定向AJAX操作。如果您希望浏览器在AJAX操作中重定向,则需要通过javascript进行操作。显然使用AJAX重定向绝对没用。如果您打算重定向使用普通的Html.Begin表单而不打扰AJAX。

#2


1  

Why don't you call the PreviewAndSendMailEmail directly? Since you don't need the redirection itself (change in url,etc), there is no need to use it.

你为什么不直接调用PreviewAndSendMailEmail?由于您不需要重定向(更改URL等),因此无需使用它。