I am experiencing the same problem as many people have asked about before, where Ajax.BeginForm is not sending the call as ajax, i.e. Request.IsAjaxRequest() == false, but not had any luck with the suggested solutions (missing js files, set 'UnobtrusiveJavascript' etc.)
My _Layout.cshtml file looks like this:
<script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
Web.config
<add key="webpages:Version" value="1.0.0.0"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
View
开发者_StackOverflow @using (Ajax.BeginForm("AjaxTest", new AjaxOptions { UpdateTargetId = "result" }))
{
<button class="btn" onclick="this.form.submit();">Hit Me!</button>
}
<div id="result"></div>
Controller
[HttpPost]
public ActionResult AjaxTest(FormCollection formCollection)
{
bool isAjax = Request.IsAjaxRequest();
return Content("Hello World");
}
I have run it through Firebug and don't get any errors.
What am I doing wrong here?
You are submitting the form yourself using onclick="this.form.submit()"
.
Remove that bit. At least give a chance to AjaxForm to show some colors :)
I'm not sure that you should have that code in the onclick. Just set the button type to be submit
精彩评论