/ / Come fare clic su collegamento ajax? - c #, ajax, asp.net-mvc

Come fare clic su collegamento ajax? - c #, ajax, asp.net-mvc

AjaxOptions ajaxMainArea = new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = "main_area" };
@Ajax.ActionLink("new game", "Game", ajaxMainArea)

Ho bisogno di fare clic su javascript.

function newgame(cost) {
//here I need call ajax method
}

Come fare questo?

risposte:

0 per risposta № 1
@Ajax.ActionLink("new game", "Game",null, ajaxMainArea,new { @id="aNewGame" })

e nel tuo script, associa la funzionalità al tuo elemento.

$(function () {
$("#aNewGame").click(function (e) {
e.preventDefault();
$.get("YourController/ActionMethod", { yourData: "someValue" }, function (response) {
//Do whatever with the reponse.
});
});
});

Supponendo che jQuery sia caricato nella tua pagina.

Se sto facendo qualcosa di personalizzato con link del genere, userò semplicemente il normale ActionLink Helper HTML e associare la funzionalità a questo come questo

@Html.ActionLink("new game", "Game",null,new {@id="aNewGame"})

e lo Script è

$(function () {
$("#aNewGame").click(function (e) {
e.preventDefault();
$.post("YourController/ActionMethod", { yourData: "someValue" }, function (response) {
$("#main_area").html(response);
//If you want to do something else, you can do it here. :)
});
});
});