C# 如何修复子操作不允许执行重定向操作,其他答案无法修复

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13896307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:03:56  来源:igfitidea点击:

How to fix Child actions are not allowed to perform redirect actions, other answers does not fix

c#.netasp.net-mvcasp.net-mvc-2

提问by Andrus

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:

结帐控制器:

    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' 属性,但问题仍然存在。

采纳答案by OakNinja

Instead of Calling public ActionResult CheckoutCompleteOK()on post, remove that action and Create a HTTP Post Action for public ActionResult PaymentByBankTransfer().

不是public ActionResult CheckoutCompleteOK()在 post上调用,而是删除该操作并为public ActionResult PaymentByBankTransfer().

Then return RedirectToAction("Complete");in PaymentByBankTransfer post method.

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

I think this would solve your problem.

我认为这将解决您的问题。

回答by MortezaDalil

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. for example I changed my child action view like this :

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

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”操作中,一切都会起作用。