I am successfully using @Ajax.ActionLink to refresh data on a portion of my page, but I would like to do the same from Javascript. How can I simulate the effects of clicking that ActionLink in js?
Thanks!
@Ajax.ActionLink("ClickMe", "List", "Organizations", New AjaxOptions With {.UpdateTargetId = "dashboardDetails"})
You need to look at using the $.get() and .$post() jQuery functions. So basically, you can perform a call to a controller action, from Javascript, using either of these functions (depending on whether your getting or posting data).
An example can be found here.
Behind the scenes Unobtrusive,JQuery simply matches on the ajax links with a[data-ajax=true]
and runs this code:
$(document).on("click", "a[data-ajax=true]", function (evt) {
evt.preventDefault();
asyncRequest(this, {
url: this.href,
type: "GET",
data: []
});
});
asyncRequest
simply runs an $.ajax call with all the options assembled for it.
You can get the same effect by simply sending a click to your link. Assuming you give your link an id with the optional HtmlAttributes like this:
@Ajax.ActionLink("ClickMe", "List", "Organizations", New AjaxOptions With {.UpdateTargetId = "dashboardDetails"}, new { id = "myajaxLink" })
you can simply trigger it with:
$('#myajaxLink').click();
JQuery has extensive support for ajax calls.
http://api.jquery.com/jQuery.ajax/
also check this blog out
Using jQuery ajax to load a partial view in ASP.NET MVC 2 and then retrieve the input on the server again