不能简单的ASP。NET MVC3表单使用AJAX异步发布

时间:2022-10-08 09:36:09

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?

我不明白为什么下面的表单执行一个完整的回发,而不是异步地使用AJAX。Request.IsAjaxRequest()总是错误的。我想我已经正确地理解了所有的例子。我做错了什么?

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();
        }
    }

}

}

1 个解决方案

#1


2  

For reference:

供参考:

AJAX and MVC 3

AJAX和MVC 3

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

Do you see this in you web.config?

在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>

#1


2  

For reference:

供参考:

AJAX and MVC 3

AJAX和MVC 3

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

Do you see this in you web.config?

在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>