1

I have a search page on an ASP.NET MVC5 application. There is a dropdown in the corner and if you select "User Search" the search filters for users appears. If you select "Task Search" the search filters for task shows.

These filters are being loaded in on partial views and are encapsultated in an Ajax.BeginForm. So when an option is chosen, it loads an entirely new form.

@using (Ajax.BeginForm("SearchUsers", "Search", "Search_default",
    new AjaxOptions
    {
        HttpMethod = "POST",
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "searchResults",
        OnBegin = "StartSpinner",
        OnComplete = "EndSpinner"
    }))
{
//Form
}

The searchResults div is on the page, so the forms are updating the content in a div on the main page with data in the form of another partial view.

Whichever search option I set as default, in this example users, works fine. When searching results come back and replace the div content as intended.

If I was then to switch to Task Search which would replace the above form with one similar but pointing to a different action, the partial view returned for search results appears on a different page.

I am thinking it is because I am dynamically loading the form, it no longer recognises the searchResults div but that could be way off. This can also be caused by unobstrusive.ajax.js if its not loaded however, the results work fine until I change the search form.

Lotok
  • 4,517
  • 1
  • 34
  • 44
  • 1
    use jquery.on to spin up the ajax. it will bind to dynamically added content. – Botonomous Sep 26 '14 at 13:27
  • Do you mean using `$.ajax` instead of the `Ajax.BeginForm`? – Lotok Sep 26 '14 at 13:33
  • No, check this method out: http://api.jquery.com/on/ You can bind to any event. It will execute the event even on dynamicly added content. – Botonomous Sep 26 '14 at 13:35
  • I'm already using `.on('change',function(){});` to load the new forms in when someone changes between user search and task search. The problem is the partial view being returned from a search loading to a new page (only after someone changes search type). Without removing `Ajax.Beginform` I am not sure where you mean to use it. Can you show a quick example? – Lotok Sep 26 '14 at 13:40
  • Im thinking because the form is dynamically, its probably beyond the intention of Ajax.BeginForm and I will just need to submit the search through javascript. If I was possible to use delegates with Ajax.BeginForm it would work, but from what I can see, it isn't. – Lotok Sep 26 '14 at 14:02
  • Cant you use javascript ajax instead of the MVC Helper? – Botonomous Sep 26 '14 at 14:04
  • yeah I can, looks like I will need to. Stupidly used the helper to save time. *rolleyes*. Going to just submit with javascript/jquery instead rather than delay more. Thanks for having a look. – Lotok Sep 26 '14 at 14:05

0 Answers0