[英]How to fix Child actions are not allowed to perform redirect actions, other answers does not fix
ASP.NET MVC2 view: ASP.NET MVC2视图:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcMusicStore.ViewModels.PaymentViewModel>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
...
<form action="<%= Html.Action("PaymentByBankTransfer", "Checkout") %>" >
<input type="submit" value="Payment by bank transfer" />
</form>
CheckoutController: CheckoutController:
public ActionResult PaymentByBankTransfer()
{
var order = Session["Order"] as Order;
ExecCommand(@"update dok set confirmed=true where order={0}", order.OrderId);
return CheckoutCompleteOK();
var cart = ShoppingCart.GetCart(HttpContext);
cart.EmptyCart();
// https://stackoverflow.com/questions/1538523/how-to-get-an-asp-net-mvc-ajax-response-to-redirect-to-new-page-instead-of-inser?lq=1
return JavaScript("window.location = '/Checkout/CompleteOK'");
}
// common method called from other controller methods also
public ActionResult CheckoutCompleteOK()
{
var cart = ShoppingCart.GetCart(HttpContext);
cart.EmptyCart();
// prevent duplicate submit if user presses F5
return RedirectToAction("Complete");
}
public ActionResult Complete()
{
var order = Session["Order"] as Order;
SendConfirmation(order);
return View("PaymentComplete", order);
}
pressing form submit button causes exception 按表单提交按钮导致异常
Child actions are not allowed to perform redirect actions
As code shows most upvoted answer from 如代码所示,来自
How to get an ASP.NET MVC Ajax response to redirect to new page instead of inserting view into UpdateTargetId? 如何获得ASP.NET MVC Ajax响应以重定向到新页面,而不是将视图插入UpdateTargetId?
is tried to fix it, but this causes other error: browser tries to open url window.location = '/Checkout/CompleteOK'
试图修复它,但这会导致其他错误:浏览器尝试打开URL
window.location = '/Checkout/CompleteOK'
How to fix this exception? 如何解决此异常? Everything looks OK, there is no partial views as described in other answers.
一切看起来都不错,没有其他答案中所述的局部视图。 I tried als o to use method='post' attribute in form but problem persists.
我尝试也可以在表单中使用method ='post'属性,但问题仍然存在。
Without using javascript for redirect: If you put forms inside your child view,Sometimes if you specify action name and controller name in Beginform helper(inside child view), this problem doesn't happen. 无需使用javascript进行重定向:如果将表单放在子视图内,有时,如果在Beginform helper(在子视图内)中指定动作名称和控制器名称,则不会发生此问题。 for example I changed my child action view like this :
例如,我像这样更改了子操作视图:
Before : 之前:
@using (Html.BeginForm())
{
...
}
After : 之后:
@using (Html.BeginForm("InsertComment", "Comments", FormMethod.Post, new { id = "commentform" }))
{
...
}
Now, You can put RedirectAction command inside "InsertComment" action and everything will work. 现在,您可以将RedirectAction命令放入“ InsertComment”操作中,然后一切正常。
Instead of Calling public ActionResult CheckoutCompleteOK()
on post, remove that action and Create a HTTP Post Action for public ActionResult PaymentByBankTransfer()
. 与其在发布时调用
public ActionResult CheckoutCompleteOK()
, public ActionResult PaymentByBankTransfer()
删除该操作并为public ActionResult PaymentByBankTransfer()
创建一个HTTP Post Action。
Then return RedirectToAction("Complete");
然后返回
RedirectToAction("Complete");
in PaymentByBankTransfer post method. 在PaymentByBankTransfer发布方法中。
I think this would solve your problem. 我认为这可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.