title: Redirect after save in partial views
body: <p>Say the requirement is to redirect to another page after successfully saving a model in an ASP.NET MVC controller:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Index(ViewModel viewModel)
  {
     if (ModelState.IsValid)
     {
          // Do save;
  
          return RedirectToAction("whatever"); // &lt;--- here's the problem 
      }
  
      // display validation errors and so
      return View(viewModel);
  }
  </code></pre>
  
  <p>This would work fine unless the controller was rendered as a Child Action:</p>
  
  <pre><code>@{
      Layout = "my layout";
  
      Html.RenderAction("something else, a view for example");
      Html.RenderAction("Index action of the above controller"); // &lt;----
  }
  </code></pre>
  
  <p>In this case RedirectResult class would check and see that the context is a child action and would throw the exception: "Child actions are not allowed to perform redirect actions".</p>
  
  <p>I understand that writing to Response stream is already in progress and a redirect cannot take place here but nevertheless one has to be able to redirect to other pages after a server-side action in a controller even if that action is a child action. We use Layouts and RenderActions to reuse the shared parts of the page design.</p>
  
  <p>How would you implement such a redirect in such a controller? </p>
  
  <p>Edit:</p>
  
  <p>The main goal is to reuse View/Controllers that do a specific job and split them to logical concepts like displaying some data or providing an edit form. My approach here is to use RenderAction to render their result into a container. Container view (main page's Index action) acts as a traditional asp.net Page, its Layout view as a Master page and edit and view controller/views are equivalent to User Controls (modules). The problem is there is no way to Redirect the Response after something has been written to it:</p>
  
  <p><img src="https://i.stack.imgur.com/gvBzf.png" alt="MVC Redirect after save problem"></p>