【问题标题】:Ajax actionlink not seem to be working for meAjax actionlink 似乎对我不起作用
【发布时间】:2017-07-01 22:56:06
【问题描述】:

我有以下标记:

<ul class="nav nav-tabs">                    
  <li>@Ajax.ActionLink("Home", "Index", "Home", new AjaxOptions { UpdateTargetId = "updaterDiv", OnSuccess = "function() {alert('onsuccess fired'); }"}) </li>
...
<div id="updaterDiv" style=" height: 200px; width: 200px;">

</div>

HomeController 中的 Index 动作

 public ActionResult Index()
    {
        if (Request.IsAjaxRequest())
        {
            return PartialView("_RefreshedHandler");
        }

        return View();
    }

当我单击链接时,AjaxOptions 中的 OnSuccess 事件没有被触发。我究竟做错了什么? 我想要实现的是避免在单击链接时在整个页面上进行回发和更新,而是只更新定义的部分

【问题讨论】:

  • 调试控制台有错误吗?
  • 不,没有错误。操作链接的行为不像我希望的那样,它仍然会回发并完全刷新页面。但我认为这是因为它不充当 Ajax actionLink,因为它不涉及 AjaxOptions
  • 完全刷新通常表示jquery.unobstrusive-ajax.js脚本没有正确加载。
  • 此answer 用于BeginForm,但也适用于ActionLink。

标签: asp.net-mvc asp.net-ajax


【解决方案1】:

我的问题的答案和解决方案在这里找到:MVC5, AjaxHelper, and the Correct Scripts & Load Order,感谢 Jasen 指向此链接。

我已经安装了 nuget 打包管理器:&gt; Install-Package Microsoft.jQuery.Unobtrusive.Ajax -Version 3.1.2,然后添加了:

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

到我的项目,然后它起作用了。运行包管理器后,jquery.unobstrusive-ajax.js 和 jquery.unobstrusive-ajax.min.js 被自动添加到项目中(在 Scripts 文件夹下)

【讨论】:

    【解决方案2】:

    需要将 MicrosoftAjax.js 和 MicrosoftMvcAjax.js 添加到您的视图中。 两者都可以在 Nuget 包管理器中找到

    Ajax.ActionLinks 不再依赖于 jquery.unobtrusive-ajax.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多