Ajax.ActionLink triggering from Javascript?

2019-08-09 02:08发布

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"})

3条回答
Lonely孤独者°
3楼-- · 2019-08-09 02:54

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.

查看更多
够拽才男人
4楼-- · 2019-08-09 03:08

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();
查看更多
登录 后发表回答