开发者

Can't get simplest ASP.NET MVC3 form to post asynchronously using AJAX

开发者 https://www.devze.com 2023-03-08 09:04 出处:网络
I can\'t see why the following form performs a full postback instead of asynchronously using AJAX.Request.IsAjaxRequest() is always false.I think I\'ve followed all the examples correctly.What am I do

I can't see why the following form performs a full postback instead of asynchronously using AJAX. Request.IsAjaxRequest() is always false. I think I've followed all the examples correctly. What am I doing wrong?

Here's the view:

@(Layout = null)
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <script src="../../Scripts/MicrosoftAjax.js" type="开发者_开发百科text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-1.4.4.js" type="text/javascript"></script>    
</head>
<body>
    <div>
        <div id="update"></div>
        @using(Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "update" }))
        {
            <input type="submit" value="test" />
        }
    </div>
</body>
</html>

And here's the controller:

using System.Web.Mvc;

namespace TheHoges.Web.Controllers
{
    public class TestController : Controller
    {
        public ActionResult Index()
        {
            if (Request.IsAjaxRequest())
            {
                // never gets here
                return Content("it worked");
            }
            return View();
        }
    }

}


For reference:

AJAX and MVC 3

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

Do you see this in you web.config?

<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

If, yes, including the library above should solve your problem.

Just to be thorough I created a test page. Works peachy...

    @{
    ViewBag.Title = "Home Page";
}

@(Layout = null)
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
</head>
<body>
    <div>
            <div id="update">
            </div>    
        @using(Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "update" }))
        {                       
            <input type="submit" value="test" />
        }
    </div>
</body>
</html>
0

精彩评论

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