2012-04-23 154 views
0
AjaxOptions ajaxMainArea = new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = "main_area" }; 
@Ajax.ActionLink("new game", "Game", ajaxMainArea) 

我需要點擊javascript。如何點擊ajax鏈接?

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

如何做到這一點?

回答

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

並且在您的腳本中,將功能綁定到您的元素。

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

假設你在頁面中加載了jQuery。

如果我做一些定製的東西,有了這樣的聯繫,我將只使用正常ActionLink HTML輔助和功能綁定到這樣

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

和腳本是

$(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. :) 
     }); 
    }); 
});