MVC 3 Ajax.ActionLink无法正常工作

时间:2011-05-20 07:53:48

标签: asp.net-mvc asp.net-mvc-3 asp.net-ajax unobtrusive-javascript actionlink

我正在玩一些基于MvcMusicStore示例的商店,并且在使用MVC3 Ajax.ActionLink / Ajax.RouteLink助手时遇到了一些问题。问题是它根本不生成Ajax请求(Request.IsAjaxRequest()== false)。我使用Ajax.BeginForm / Ajax.BeginRouteForm生成的表单虽然很好用。

配置:

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

脚本:

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

链接:

@Ajax.ActionLink("show cart", "Show", "Cart", new AjaxOptions() { OnSuccess = "handleSuccess", HttpMethod = "Get", OnFailure = "handleFailure" })

生成HTML:

<a data-ajax="true" data-ajax-failure="handleFailure" data-ajax-method="Get" data-ajax-success="handleSuccess" href="/Cart/Show">show cart</a>

如上所述,这很好用:

@using (Ajax.BeginForm(
        "Show",
        new { controller = "Cart" },
        new AjaxOptions
        {
            OnSuccess = "handleSuccess",
            OnFailure = "handleFailure"
        }))
    {
        <input type="submit" class="button" />
    }

动作如下:

[Authorize]     
public ActionResult Show()
{
    if (Request.IsAjaxRequest())
    {
        ViewBag.CartItems = ShoppingCart.GetCart(this)
            .Items;

        return Json(new AjaxResultViewModel()
        {
            Content = RenderPartialViewToString(),
            UpdateTargetSelector = "#dialog",
            InsertionMode = InsertionMode.InsertBefore
        }, JsonRequestBehavior.AllowGet);
    }

    ViewBag.Exception = new NotSupportedException();
    return View("Error");
}

我一直在寻找一段时间而无法找到这种行为的原因,也许有人可以帮我解决?

祝你好运!

2 个答案:

答案 0 :(得分:10)

在靠近底部的_layout.cshtml上的新MVC4模板中,有一个@Scripts.Render("~/bundles/jquery")

我必须包括 @Scripts.Render("~/bundles/jqueryval")这是Ajax库

这解决了我使用索引页面进行完整回发时遇到的问题。我的操作链接代码:

<div id="timeDisplay">
@Ajax.ActionLink("Click to display server time", "Time", new AjaxOptions { HttpMethod="GET",UpdateTargetId="timeDisplay"});
</div>

答案 1 :(得分:2)

我通过按照这个确切的顺序声明以下脚本来实现它:

@section script
{
  <script src="@Url.Content("~/Scripts/MicrosoftAjax.debug.js")" type="text/javascript"></script>
  <script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js")" type="text/javascript"></script>
  <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript">   </script>
}

为了完整起见,在我的* _Layout.cshtml *文件中,我声明了以下内容:

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

@RenderSection("script", required:false)

<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>