title: ASP.NET MVC 4 - Redirect after Modal Save using Child Action
body: <hr>
  
  <p><strong>UPDATE</strong> - There are a lot of posts regarding the <strong>Child actions are not allowed to perform redirect actions</strong> on SO and ASP.NET forums.  The solution below seems to be the simplest workaround that I've seen yet for this problem.  If you have this issue - hopefully you'll find it helpful.  If there is a better one, I'm certainly open to suggestions.</p>
  
  <hr>
  
  <p>I've been wracking my brain with this all day...</p>
  
  <p>I have a <strong>FranchiseSet</strong> model and a <strong>Franchise</strong> model - the relationship is that a FranchiseSet has-many Franchises.</p>
  
  <p>On the FranchiseSet index view there is a table of Franchise Sets where each row is clickable.  When I click a row, I return a view from the <strong>Details</strong> method of the <strong>FranchiseSetController</strong> - through AJAX.</p>
  
  <p>Within that view there is a also a table that shows all related Franchises for that Franchise set.  Essentially, it is a <strong>Master/Detail</strong> View - Here is what that looks like:</p>
  
  <p><img src="https://i.stack.imgur.com/tA3yd.png" alt="enter image description here"></p>
  
  <p>There is a Twitter Bootstrap modal being used to display the Create form for franchises.  Here's what that looks like:</p>
  
  <pre><code>&lt;div class="modal hide fade in" id="myModal" )&gt;
        &lt;div class="modal-header"&gt;
          &lt;button type="button" class="close" data-dismiss="modal"&gt;×&lt;/button&gt;
          &lt;h3&gt;Modal header&lt;/h3&gt;
        &lt;/div&gt;
        &lt;div id="create_franchise_modal_body" class="modal-body"&gt;
          @Html.Action("Create", "Franchise", new { FranchiseSetId = Model.FranchiseSetID })       
        &lt;/div&gt;
        &lt;div class="modal-footer"&gt;
          &lt;a href="#" class="btn" data-dismiss="modal"&gt;Close&lt;/a&gt;
          &lt;a href="#" class="btn btn-primary"&gt;Save changes&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>Inside the body of the modal, I'm using an <code>@Html.Action</code> helper to call the <strong>FranchiseController's Create</strong> method:</p>
  
  <pre><code>@Html.Action("Create", "Franchise", new { FranchiseSetId = Model.FranchiseSetID })
  </code></pre>
  
  <p>Here is the modal that is generated from that:</p>
  
  <p><img src="https://i.stack.imgur.com/p5Kut.png" alt="enter image description here"></p>
  
  <p>The Create method is successfully saving the new Franchise, however, I get this error:</p>
  
  <p><strong>Child actions are not allowed to perform redirect actions.</strong></p>
  
  <p>I've seen <strong>LOTS</strong> of posts on StackOverflow and the ASP.NET Forums on this issue, and I've tried to implement some of the solutions - even using JavaScript redirects, but I still haven't been able to get this to work.</p>
  
  <p>I understand that since I am already inside of the FranchiseSet Controller, that it is already trying to render a view based on the FranchiseSet model, so the Child view - of the Franchise model - is undefined.  So what is the ideal way to deal with interactions among parent and child actions?  </p>
  
  <p>This seems to be a very common problem without a common solution.  Or at least not one that I can find.  I'm relatively new to .net mvc so it may just be me.</p>
  
  <p>Is using <code>@Html.Action</code> a bad design choice in this scenario?  I have tried to use <code>@Html.RenderPartial</code> but that causes a 500 Internal Server error - even if I have the Create method setup to return a partial view.</p>
  
  <p>Here are the FranchiseController Create actions - both GET and POST - that I am using:</p>
  
  <pre><code>//
  // GET: /Franchise/Create
  
  public ActionResult Create(int FranchiseSetId)
  {
      ViewBag.PossibleFranchiseSets = franchisesetRepository.All;
      var franchise = new Franchise { FranchiseSetId = FranchiseSetId };
      return PartialView(franchise);
  } 
  
  //
  // POST: /Franchise/Create
  
  [HttpPost]
  public ActionResult Create(Franchise franchise)
  {
      if (ModelState.IsValid) {
          franchiseRepository.InsertOrUpdate(franchise);
          franchiseRepository.Save();
          return RedirectToAction("Index", "FranchiseSet");
      } else {
          ViewBag.PossibleFranchiseSets = franchisesetRepository.All;
          return View();
      }
  }
  </code></pre>
  
  <p>The <code>return RedirectToAction("Index", "FranchiseSet");</code> in conjunction with the <code>@Html.Action</code> from the view seems to be the source of the problem.</p>
  
  <p><strong>Any advice is much appreciated!</strong></p>
  
  <hr>
  
  <p><strong>SOLUTION</strong></p>
  
  <hr>
  
  <p>I used the modified modal code that Russell suggested below, however I switched the @Html.RenderPartial method for the @Html.RenderAction method within a code block.</p>
  
  <p>Here is what it looks like now.</p>
  
  <pre><code>&lt;div class="modal hide fade in" id="myModal" )&gt;
        &lt;div class="modal-header"&gt;
          &lt;button type="button" class="close" data-dismiss="modal"&gt;×&lt;/button&gt;
          &lt;h3&gt;Modal header&lt;/h3&gt;
        &lt;/div&gt;
        @using (Html.BeginForm("Create", "Franchise", FormMethod.Post))
        {
           &lt;div id="create_franchise_modal_body" class="modal-body"&gt;
             @{ Html.RenderAction("Create", "Franchise", new { FranchiseSetId = Model.FranchiseSetID }); }
           &lt;/div&gt;
           &lt;div class="modal-footer"&gt;
             &lt;a href="#" class="btn" data-dismiss="modal"&gt;Close&lt;/a&gt;
             &lt;button type="submit" class="btn btn-primary"&gt;Save changes&lt;/button&gt;
           &lt;/div&gt;
        }
    &lt;/div&gt;
  </code></pre>
  
  <p>I also modified the controller's Create method like Russell suggested above.</p>
  
  <p>Using this:  <code>@{ Html.RenderAction("Create", "Franchise", new { FranchiseSetId = Model.FranchiseSetID }); }</code></p>
  
  <p>I'm putting it within the <code>@{ }</code> tags because of what I learned here:  <a href="https://stackoverflow.com/questions/5435893/html-renderpartial-giving-me-strange-overload-error">Html.RenderPartial giving me strange overload error?</a></p>
  
  <p>I was able to get the FranchiseController to redirect back to its parent - the FranchiseSetController's Index method - with no problems.  Plus it didn't require me to return JSON and try to do a JavaScript redirect - it's all Razor view code.</p>
  
  <p>I hope this is solves the problem of how to Redirect from <strong>Child</strong> actions back to the <strong>Parent</strong>.</p>