2

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.

Community
  • 1
  • 1
Andrus
  • 26,339
  • 60
  • 204
  • 378
  • It is not related to your question, but do you realize that in your PaymentByBankTransfer action the code after the first return is not reachable? – Romias Dec 15 '12 at 21:40
  • yes. Code after first return shows another unsuccesful attempt to resolve the issue using code from other, referenced answer. It is better to avoid this javascipt injection, first return should work. I asked why first return causes this stange error, probably in RedirectToAction method call – Andrus Dec 15 '12 at 22:02
  • Have you tried `return RedirectToAction("Complete");` directly from `PaymentByBankTransfer()` ? Do you have the same error or do you achieve the desired behavior ? – Yan Brunet Dec 15 '12 at 22:14
  • I tried but still got `Child actions are not allowed to perform redirect actions.` exception. – Andrus Dec 15 '12 at 22:21
  • They "complete" action, renders a PartialView or a View? – Romias Dec 15 '12 at 22:39
  • It renders View. I updated question and added Complete method code. – Andrus Dec 16 '12 at 09:41
  • I had this problem and after many search and tries I found the problem is in related controller method (calling action same as PaymentByBankTransfer here). It had an error but not thrown proper exception and I just got famous error "Child actions are not allowed to perform redirect actions", So after handle and repair the error problem gone away. – QMaster Jul 31 '17 at 16:53

2 Answers2

4

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.

Satpal
  • 132,252
  • 13
  • 159
  • 168
OakNinja
  • 2,326
  • 18
  • 20
4

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.

MortezaDalil
  • 352
  • 1
  • 2
  • 16