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

孤人 提交于 2019-12-03 14:15:06

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/13896307/how-to-fix-child-actions-are-not-allowed-to-perform-redirect-actions-other-answ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!