Search code examples
asp.net-mvc-4ajax.beginform

MVC - why code after ajax.beginform being executed


Form for AJAX call:

@using(Ajax.BeginForm("RefineSearchResults", "Search", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "resultsSec" }))
{
<input type="submit" value="submit" />
<div id="resultsSec"></div>
}

after form tag:

@{Html.RenderAction("Index", "NewsLetter", new { area = "" });}

but, it throws exception on second piece of code when posted back although it's not supposed to be execued because it's an AJAX call and it's outside the Ajax form.

Exception message:

Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.

Can anyone please tell me what's going wrong here. Thanks!


Solution

  • oops!!! My mistake! Actually, I was returning the main view again that's why code that wasn't supposed to be rendered was being rendered.

    Now I have added the view code that was to be refereshed through Ajax.BeginForm() to a partial view.

    My openion:

    You should apply Ajax form on partial view preferably because from controller when you return a view then the target view have to be rendered again (it's ok when you have to show only a string through Content() method.)

    So, refreshing the partial view through Ajax form would be an ideal way to use Ajax.BeginForm() in my humble openion.