开发者

ASP.NEW MVC3 Ajax BeginForm not sending as Ajax

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜