简体   繁体   English

Ajax.ActionLink 从 Javascript 触发?

[英]Ajax.ActionLink triggering from Javascript?

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.我成功地使用@Ajax.ActionLink 来刷新我页面的一部分上的数据,但我想从 Javascript 做同样的事情。 How can I simulate the effects of clicking that ActionLink in js?如何在js中模拟点击那个ActionLink的效果?

Thanks!谢谢!

@Ajax.ActionLink("ClickMe", "List", "Organizations", New AjaxOptions With {.UpdateTargetId = "dashboardDetails"})

You need to look at using the $.get() and .$post() jQuery functions.您需要查看使用$.get().$post() jQuery 函数。 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).所以基本上,您可以使用这些函数中的任何一个(取决于您是获取还是发布数据)从 Javascript 执行对 controller 操作的调用。

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:在幕后不引人注目,JQuery 只需在 ajax 链接上匹配a[data-ajax=true]并运行此代码:

$(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. asyncRequest只需运行 $.ajax 调用,并为其组装所有选项。

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:假设您为链接提供了一个带有可选 HtmlAttributes 的 id,如下所示:

@Ajax.ActionLink("ClickMe", "List", "Organizations", New AjaxOptions With {.UpdateTargetId = "dashboardDetails"}, new { id = "myajaxLink" })

you can simply trigger it with:你可以简单地触发它:

$('#myajaxLink').click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM