【问题标题】:How to fix Child actions are not allowed to perform redirect actions, other answers does not fix如何修复子动作不允许执行重定向动作,其他答案不修复
【发布时间】:2012-12-03 11:52:54
【问题描述】:

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>

结帐控制器:

    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);
     }

按下表单提交按钮会导致异常

Child actions are not allowed to perform redirect actions

由于代码显示来自

的最受好评的答案

How to get an ASP.NET MVC Ajax response to redirect to new page instead of inserting view into UpdateTargetId?

试图修复它,但这会导致其他错误:浏览器尝试打开 url window.location = '/Checkout/CompleteOK'

如何解决这个异常?一切看起来都不错,没有其他答案中描述的部分视图。 我也尝试在表单中使用 method='post' 属性,但问题仍然存在。

【问题讨论】:

  • 这与您的问题无关,但是您是否意识到在您的 PaymentByBankTransfer 操作中,第一次返回后的代码无法访问?
  • 是的。首次返回后的代码显示使用其他引用答案中的代码解决问题的另一次失败尝试。最好避免这种 javascipt 注入,首先返回应该可以。我问为什么 first return 会导致这个 stange 错误,可能在 RedirectToAction 方法调用中
  • 你有没有直接从PaymentByBankTransfer()尝试过return RedirectToAction("Complete");?你有同样的错误还是你达到了预期的行为?
  • 我试过了,但还是得到了Child actions are not allowed to perform redirect actions. 异常。
  • 他们“完成”动作,渲染一个 PartialView 还是一个 View?

标签: c# .net asp.net-mvc asp.net-mvc-2


【解决方案1】:

不要在发布时调用 public ActionResult CheckoutCompleteOK(),而是删除该操作并为 public ActionResult PaymentByBankTransfer() 创建一个 HTTP 发布操作。

然后在 PaymentByBankTransfer post 方法中返回RedirectToAction("Complete");

我认为这会解决你的问题。

【讨论】:

    【解决方案2】:

    不使用 javascript 进行重定向: 如果您将表单放在子视图中,有时如果您在 Beginform 助手(子视图内部)中指定操作名称和控制器名称,则不会发生此问题。例如,我像这样更改了我的子操作视图:

    之前:

    @using (Html.BeginForm())
    {
     ...
    }
    

    之后:

        @using (Html.BeginForm("InsertComment", "Comments", FormMethod.Post, new { id = "commentform" })) 
    {
     ...
    }
    

    现在,您可以将 RedirectAction 命令放入“InsertComment”操作中,一切都会正常进行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      相关资源
      最近更新 更多