title: How to return Nested PartialViews (including their javascript) from an AJAX call in ASP.Net MVC
body: <p>I have created a treeview of Categories using nested partial views:</p>
  
  <p>my Index page (that displays the treeview):</p>
  
  <pre><code>&lt;div&gt;
   Category Menu:
    &lt;input type="button" value="1" name='selectCat_btn' /&gt;
    &lt;input type="button" value="2" name='selectCat_btn' /&gt;
  &lt;/div&gt;
  
  &lt;!-- Treeview --&gt;
  &lt;% Html.RenderPartial("ItemCats_UL", Model); %&gt;
  
  &lt;div id="CatSelectorOutput"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>ItemCats_UL:</p>
  
  <pre><code>&lt;div&gt;
   &lt;ul id="catsTree"&gt; 
    &lt;% Html.RenderPartial("ItemCats_LI", Model); %&gt;
   &lt;/ul&gt;
  &lt;/div&gt;
  
  &lt;script type="text/javascript" &gt;
  $(document).ready(function() {        
      $("#catsTree").treeview();
  &lt;/script&gt;
  </code></pre>
  
  <p>ItemCats_LI:</p>
  
  <pre><code>&lt;%foreach (ItemCategory itemCat in Model)
   { %&gt;
    &lt;li&gt;
     &lt;%= itemCat.Name %&gt;
      &lt;%if (itemCat.Children != null &amp;&amp; itemCat.Children.Count() &gt; 0)
        { %&gt;
         &lt;ul&gt;
          &lt;% Html.RenderPartial("ItemCats_LI", itemCat.Children); %&gt;
         &lt;/ul&gt;
      &lt;%} %&gt;
   &lt;/li&gt;
  &lt;%} %&gt;
  </code></pre>
  
  <p>Now this treeview works perfectly when I return the basic View("Index", Model) from my controllers Index action on page load. </p>
  
  <p>The <strong>trouble comes when I want to change the Categories Model displayed in my Treeview (the nested partialViews) from an AJAX call</strong>...</p>
  
  <p>For example: I click one the 'Cats2' button and the page should display Categories with ParentID of 2 in the Treeview. I attempted this by returning a JsonResult of the html of the ItemCats_UL PartialView (using a <strong>RenderPartialToString</strong> method <a href="http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/" rel="nofollow noreferrer">found here</a>) from my Controller Action. As some of you might know Javascript won't run in your partial view when you use an AJAX form to return a PartialViewResult, and I need Javascript in my Treeview which is why I'm using the RenderPartialToString.</p>
  
  <p>The category select button click handler:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $("[name='selectCat_btn']").click(function() {       
      var CID = $(this).attr('value');      
      $.ajax({
          type: "POST",
          url: "SelectCat",
          dataType: "json",
          data: { "CID": CID },
          success: function(result) { $("#CatSelectorOutput").html(result.output); }
      });
      return false;
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>My Controller Action:</p>
  
  <pre><code> [AcceptVerbs(HttpVerbs.Post)]
      [UrlRoute(Name = "SelectCat", Path = "selectCat")]
      public ActionResult SelectCat(int CID)
      {
          IQueryable&lt;ItemCategory&gt; cats;
          cats = ItemRepo.GetItemCats().WithCID(CID);
  
          JsonResult result = null;
          result = new JsonResult
          {
              Data = new
              {
                  success = true,
                  output =
                  Helpers.RenderHelper
                  .RenderPartialToString("~/Views/Admin/AdminItemCatsUL.ascx",
                  cats)                    
              }
          };
          return result;
      }
  </code></pre>
  
  <p><strong>The result:</strong></p>
  
  <p>The ItemCats<code>_UL partialView displays! BUT the nested PartialViews (ItemCats_</code>LI) don't!</p>
  
  <p>Error I receive when I step through the markup in the ItemCats_UL.ascx and hover over the 'Html' part of the following code:</p>
  
  <pre><code>&lt;ul id="catsTree"&gt; 
   &lt;% Html.RenderPartial("ItemCats_LI", Model); %&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p><strong>Value cannot be null.
  Parameter name: viewContext</strong><br>
  <strong>Html = 'Html' threw an exception of type 'System.ArgumentNullException'</strong><br>
  I'm wondering if there's a clever guy out there who can extend the RenderPartialToString method to include nested partialviews? Or am I missing something simple?</p>