Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 + Razor Error: Child actions are not allowed to perform redirect actions

So I saw this question here on SO, but it hasn't really solved this problem for me.

I have an ASP.NET MVC 3 + Razor app running on IIS5 on my dev pc, and then IIS6 for my dev web server. Everything worked great until I deployed it. I am bin deploying everything, with no problems on that front (that I can tell).

Now I am getting this Child actions are not allowed to perform redirect actions error on my page. I am not sure how to pinpoint where it is failing.

I'm using @Html.Action to pull in some drop down boxes with data:

    public ActionResult Hierarchy()
    {
        List<Store> storeList = DBService.getStores();            
        if (DBService.Error != null)
            return RedirectToError(DBService.Error);

        List<Department> deptList = DBService.getDepts();
        if (DBService.Error != null)
            return RedirectToError(DBService.Error);

        VM_Hierarchy hierarchy = new VM_Hierarchy(storeList, deptList);

        return View(hierarchy);
    }

If I remove the @Html.Action line, the page will render. It will then break if I do an AJAX request to a controller action such as this:

    [HttpPost]
    public ActionResult InventoryList(string fromDate, string toDate)
    {
        List<Inventory> inventories = DBService.getInventories(fromDate, toDate);

        if (DBService.Error != null)
            return RedirectToAction("Error");

        return View(inventories);
    }

If this isn't correct, how am I supposed to redirect to an error page or dictate what view gets returned on a post? Any help is appreciated. Thanks.

like image 230
IronicMuffin Avatar asked Apr 13 '11 15:04

IronicMuffin


3 Answers

It's probably because you're getting errors from your DB service when it's deployed, which you aren't getting locally. This is causing it to try and redirect from a child action, which you can't do.

You might want to try getting the drop-down data in your main action, and put it all in a ViewModel, so you aren't using child actions. If this fails, you can redirect to your heart's content. For the AJAX, you'll need to handle the errors on the client and do something sensible, you can't just return a redirection from an AJAX call.

This question has some further info: Why are Redirect Results not allowed in Child Actions in Asp.net MVC 2

like image 102
Grant Crofton Avatar answered Nov 11 '22 06:11

Grant Crofton


if you put in the child action (ControllerContext.ParentActionViewContext.Controller as System.Web.Mvc.Controller).Response.Redirect(Url.Action("Action", "Controller")); it will redirect from the childaction.

like image 44
Tom Maxwell Avatar answered Nov 11 '22 05:11

Tom Maxwell


I had a similar problem but for the life of me, I couldn't figure out where the redirection was coming from. Once I figured it out, I thought I'd post an answer in case it happens to anyone else.

If the source of the redirection is not apparent from the imperative code, e.g.:

    [AuthoriseThis]
    public ActionResult Details(SomethingModel something)
    {
        return PartialView(something);
    }

Look at the declarative code!

In my case, it was an authorisation problem, so the attribute was causing the redirection, and my eyes were filtering out the existence of the attribute.

like image 1
David Avatar answered Nov 11 '22 05:11

David