1

I have the following markup:

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

And the Index action in the HomeController

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

        return View();
    }

The OnSuccess event in the AjaxOptions is not getting fired when I click on the link. What am I doing wrong? What I am trying to achieve, is to avoid doing a postback and update on the entire page when the link is clicked, but rather only update defined a section

user1960836
  • 1,732
  • 7
  • 27
  • 47
  • Are there any errors on the debug console? – Jasen Feb 20 '15 at 00:14
  • No, no errors. The action link does not behave like I am hoping, it still makes a postback and full refresh of the page. But this I think is because it doesn't act as an Ajax actionLink, since it doesn't touch the AjaxOptions – user1960836 Feb 20 '15 at 00:23
  • 2
    Full refresh usually indicates the `jquery.unobstrusive-ajax.js` scripts are not loaded properly. – Jasen Feb 20 '15 at 00:25
  • 1
    This [answer](http://stackoverflow.com/questions/23895918/mvc5-ajax-beginform-and-the-correct-scripts-load-order/23897170#23897170) is for `BeginForm` but also applies to `ActionLink`. – Jasen Feb 20 '15 at 00:29

2 Answers2

2

The answer and solution to my problem was found here: MVC5, AjaxHelper, and the Correct Scripts & Load Order, thanks to Jasen for pointing to this link.

I have installed with the nuget packed manager: > Install-Package Microsoft.jQuery.Unobtrusive.Ajax -Version 3.1.2, and then added the:

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

to my project, then it worked. jquery.unobstrusive-ajax.js and jquery.unobstrusive-ajax.min.js were automatically added to the project (under the Scripts folder) after running the package manager

Community
  • 1
  • 1
user1960836
  • 1,732
  • 7
  • 27
  • 47
0

It is required to add MicrosoftAjax.js and MicrosoftMvcAjax.js to your view. Both may be found in Nuget Package Manager

Ajax.ActionLinks no longer depend from jquery.unobstrusive-ajax.js

intox
  • 514
  • 7
  • 14