简体   繁体   中英

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

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>

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?

is tried to fix it, but this causes other error: browser tries to open 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.

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 :

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.

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

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

I think this would solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM