9

My search functionality seems to continue in a infinite loop, everytime my debug hits the action below the POST actionresult gets fired.

In my Masterpage.cshtml I have following action:

 <li>@Html.Action("Search", "Search")</li>

This is the part that gets the error of following:

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.

In my SearchController I have one get and post actionresult methods:

[HttpGet]
        public ActionResult Search()
        {
            return PartialView("SearchFormPartial");
        }

This one returns a partial view that have following content:

@using (Ajax.BeginForm("Search", "Search", FormMethod.Post,
        new AjaxOptions
        {
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST"

         }))
{
<div>
    @Html.TextBox("query", "", new { @class = "search-query", @placeholder="Search news...", @spellcheck="false"})
    <input type="submit" value="Search" />
</div>      
}

Its basicly a form with the textbox and submit button.

This is the http post actionresult:

[HttpPost]

    public ActionResult Search(string query)
    {
        if (query != null)
        {
            try
            {

                var searchlist = rep.Search(query);

                var model = new ItemViewModel()
                {
                    NewsList = new List<NewsViewModel>()
                };

                foreach (var NewsItems in searchlist)
                {
                    FillProductToModel(model, NewsItems);
                }


                return View("Searchresults", model);
            }
            catch (Exception e)
            {
                // handle exception
            }
        }
        return View("Error");


    }

It returns a view with a viewmodel that contains the items that matched the query.

When I debug it everything works perfectly but everything seems to be repeated infinitly.

The view for the Searchresult looks like this:

@model Namespace.ViewModels.ItemViewModel
@if (Model.NewsList.Count == 0)
{
    <h3 class="text-error">No items matched your search query!</h3>
}
else
{
    foreach (var result in Model.NewsList)
    {
        // display search results
    }
}

What is exacly going wrong here that cause this infinite loop? and how can I fix it?

In the stack trace I found these exceptions

[HttpException (0x80004005): Error executing child request for handler

'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.]

this exception seems getting repeated

Obsivus
  • 8,231
  • 13
  • 52
  • 97
  • nope but when the iteration is done it jumps to the masterpage.cshtml and the Html.Action("Search","Search"") is there. – Obsivus May 20 '13 at 21:42
  • Do you get the error when you post to the Search action? – Ufuk Hacıoğulları May 20 '13 at 21:42
  • I get the error when i stop running the webb app.. else its just infinite loop which means when I type a text and click on submit its just loading – Obsivus May 20 '13 at 21:43
  • There shouldn't be an issue when you call the Search action with GET request, it returns a PartialViewResult. You should get the error when you make a POST request. – Ufuk Hacıoğulları May 20 '13 at 21:48
  • When I debugg the post actionresults this is what happens: it returns a view and the goes throught everything inside the view, then moves on to the masterpage.cshtml, when it hits the Html.Action("Search","Search"") . It goes directly to the Post actionresult and so on on repeating. – Obsivus May 20 '13 at 21:51
  • which routes have you declared? after reading your last comment I think something wrong could be happening there – ppetrov May 20 '13 at 21:53
  • ill update with the routings 1 sec – Obsivus May 20 '13 at 21:53
  • wrong guess sorry, your routes are fine – ppetrov May 20 '13 at 21:57

4 Answers4

9

Html.Action in master page calls the Search method with a POST request, so the framework won't call the action that returns the partial view but the other that returns a ViewResult with the master page. Same thing will happen again and you will be making recursive calls.

Simplest solution would be to rename the Search action that responds to POST request. Also make sure your form posts to this action but keep the same Html.Action call.

It seems like framework will still try to find the action that can respond to a POST request. Removing HttpGet attribute from Search action will solve this problem.

Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
  • I renamed the post actionresult to SearchResult and inside the partialview I changed to @using (Ajax.BeginForm("SearchResult", "Search", FormMethod.Post, new AjaxOptions, but now I get error : Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. – Obsivus May 20 '13 at 22:06
  • @Obsivus Can you comment out all the code in your partial view and see if you get the same error? – Ufuk Hacıoğulları May 20 '13 at 22:15
  • if I comment out There is no textbox or submit button cant test it if it works or not – Obsivus May 20 '13 at 22:22
  • If you don't get any errors, it means something is wrong with your view. – Ufuk Hacıoğulları May 20 '13 at 22:24
  • even if I comment out the stuff in the view I am in infinite loop beacuse of this action in my masterpage – Obsivus May 20 '13 at 22:26
  • @Obsivus Remove HttpGet from Search action and use the renamed method for POST. It works for me. – Ufuk Hacıoğulları May 20 '13 at 22:36
2

Its not seeing the your Partial view as a 'Partial View'. I had exactly the same problem but adding @{ Layout = null; } to the view ensures that the view is not seen as a normal view which loads the _Layout view.

Gabriel
  • 21
  • 1
1

The issue here is actually very simple - it should be

<li>@Url.Action("Search", "Search")</li>

instead of

<li>@Html.Action("Search", "Search")</li>

See Url vs Html - @Url will generate a string of a link, while @Html will try to generate the outcome of the action (which might lead to infinite loop)

Grengas
  • 836
  • 12
  • 16
0

My issue is that I added a new view through visual studio and it added a _ViewStart.cshtml page that had a layout which was causing recursion.

Ben
  • 1,853
  • 19
  • 20