5

I got an action List

    //[HttpGet] (will come back to that!)
    public ViewResult List(int page = 1)
    {
        //blah blah blah
        return View(viewModel);
    }

In its view we render action:

@{        
    Html.RenderAction("UpdateSearch");
}

Action definitions:

[ChildActionOnly]
[HttpGet]
public PartialViewResult UpdateSearch()
{
    // do something and display a form in view
    return PartialView(so);
}

[HttpPost]
public RedirectToRouteResult UpdateSearch(Options searchOptions)
{
    // do something and redirect to List
    return RedirectToAction("List");
}

and I'm getting: Child actions are not allowed to perform redirect actions exception every time someone submits the form. I'm new to MVC3, but it looks like the redirection is also a POST, because if [HttpGet] above List method is uncommented "the resource cannot be found" happens.

How do I change Http method on redirection or am I doing something wrong? I did try to Bing it, but no success.

Mariusz.W
  • 1,347
  • 12
  • 19

1 Answers1

3

The redirect info is stored in response header. However, the response is already being sent when child action is run so headers can't be written.

In short, there's no way of performing a redirect from child action other than through the use of javascript on client side.

Vilém Procházka
  • 1,060
  • 2
  • 17
  • 28
  • That would make sense, but the redirect in my example is not done by method with [ChildActionOnly] annotations. The problem is that when [HttpPost] UpdateSearch redirects to List, it redirects with POST, hence [HttpPost] method is (trying to be) used for partial generation in view. Could I use RouteValueDictionary to change it? – Mariusz.W Mar 20 '12 at 15:34
  • Show us the content of the partial view as well. The problem probably lies in wrong route definition in BeginForm but I can't tell without seeing the view. – Vilém Procházka Mar 20 '12 at 15:52
  • 2
    You're right. To fix it, it is enough to add action parameter to BeginForm method in UpdateSearch view (Get, Child version) so a change from: /@using (Html.BeginForm()) { to: /@using (Html.BeginForm("UpdateSearch")) { fixed the problem. Are you able to explain why? – Mariusz.W Mar 20 '12 at 16:03
  • BeginForm without parameters refers to current route of the main action, not child action. Therefore what happened was that the UpdateSearch(Options searchOptions) method was called as a child action. – Vilém Procházka Mar 20 '12 at 16:33