Reputation: 27955
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
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.
Upvotes: 2
Views: 27817
Reputation: 361
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.
Upvotes: 4
Reputation: 2346
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.
Upvotes: 4