... " > 结帐 Controller : public ActionResult PaymentB-6ren">
gpt4 book ai didi

c# - 如何修复不允许子操作执行重定向操作,其他答案未修复

转载 作者:太空宇宙 更新时间:2023-11-03 18:37:01 26 4
gpt4 key购买 nike

ASP.NET MVC2 View :

<%@ 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>

结帐 Controller :

    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'

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

最佳答案

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

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

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

关于c# - 如何修复不允许子操作执行重定向操作,其他答案未修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13896307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com