title: Insufficient stack to continue executing the program safely. ASP.NET MVC 4
body: <p>My search functionality seems to continue in a infinite loop, everytime my debug hits the action below the POST actionresult gets fired.</p>
  
  <p>In my Masterpage.cshtml I have following action:</p>
  
  <pre><code> &lt;li&gt;@Html.Action("Search", "Search")&lt;/li&gt;
  </code></pre>
  
  <p>This is the part that gets the error of following:</p>
  
  <blockquote>
    <p>Insufficient stack to continue executing the program safely. This can
    happen from having too many functions on the call stack or function on
    the stack using too much stack space.</p>
  </blockquote>
  
  <p>In my SearchController I have one get and post actionresult methods:</p>
  
  <pre><code>[HttpGet]
          public ActionResult Search()
          {
              return PartialView("SearchFormPartial");
          }
  </code></pre>
  
  <p>This one returns a partial view that have following content:</p>
  
  <pre><code>@using (Ajax.BeginForm("Search", "Search", FormMethod.Post,
          new AjaxOptions
          {
              InsertionMode = InsertionMode.Replace,
              HttpMethod = "POST"
  
           }))
  {
  &lt;div&gt;
      @Html.TextBox("query", "", new { @class = "search-query", @placeholder="Search news...", @spellcheck="false"})
      &lt;input type="submit" value="Search" /&gt;
  &lt;/div&gt;      
  }
  </code></pre>
  
  <p>Its basicly a form with the textbox and submit button.</p>
  
  <p>This is the http post actionresult:</p>
  
  <pre><code>[HttpPost]
  
      public ActionResult Search(string query)
      {
          if (query != null)
          {
              try
              {
  
                  var searchlist = rep.Search(query);
  
                  var model = new ItemViewModel()
                  {
                      NewsList = new List&lt;NewsViewModel&gt;()
                  };
  
                  foreach (var NewsItems in searchlist)
                  {
                      FillProductToModel(model, NewsItems);
                  }
  
  
                  return View("Searchresults", model);
              }
              catch (Exception e)
              {
                  // handle exception
              }
          }
          return View("Error");
  
  
      }
  </code></pre>
  
  <p>It returns a view with a viewmodel that contains the items that matched the query.</p>
  
  <p>When I debug it everything works perfectly but everything seems to be repeated infinitly.</p>
  
  <p>The view for the Searchresult looks like this:</p>
  
  <pre><code>@model Namespace.ViewModels.ItemViewModel
  @if (Model.NewsList.Count == 0)
  {
      &lt;h3 class="text-error"&gt;No items matched your search query!&lt;/h3&gt;
  }
  else
  {
      foreach (var result in Model.NewsList)
      {
          // display search results
      }
  }
  </code></pre>
  
  <p>What is exacly going wrong here that cause this infinite loop? and how can I fix it?</p>
  
  <p>In the stack trace I found these exceptions </p>
  
  <blockquote>
  <pre><code>[HttpException (0x80004005): Error executing child request for handler
  </code></pre>
    
    <p>'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.]</p>
  </blockquote>
  
  <p>this exception seems getting repeated</p>