2012-11-24 27 views
1

我有我的網頁上點擊此鏈接:ActionLink應該HttpPost操作方法

@Html.ActionLink("Like", "Like", "Like", new {likeId = i.ItemId}, new {id = @i.ItemId, @class = "likeButton"}) 

這是我的Ajax調用:

$(document).on("click", ".likeButton", function (event) { 

     var itemId = event.target.id; 

     $.ajax({ 
      url: this.href, 
      type: 'POST', 
      data: { item: itemId }, 
      context: this, 
      success: function (result) { 
       ... 
     return false; 
    }); 

當action metohd就像是它的工作原理:

public ActionResult Like(int itemId) 
... 

但是,如果我用[HttpPost]裝飾方法它不起作用。
這可以實現嗎?
如果我不添加[HttpPost],又會出現什麼安全問題?

+0

您正在尋找'int itemId',您爲什麼要傳遞item:'data:{item:itemId}'? – Koste

回答

2

試試這個:

$(document).on("click", ".likeButton", function (event) { 

    $.ajax({ 
     url: this.href, 
     type: 'POST', 
     context: this, 
     success: function (result) { 
      ... 
    return false; 
}); 

你傳入item.Id兩次,第一次在URL和第二身體。當使用post方法時,你仍然可以通過url傳遞參數。當你想隱藏這些參數時,傳遞參數與body是很好的。

還有一件事,你可以在這種情況下使用Ajax.ActionLink

(因爲它是爲這種情況下創建的),你有錯:

data: { item: itemId } 

應該是:

data: { itemId: itemId }, 
+1

是的,當我從功能中刪除'數據'時,問題消失了。 – 1110

相關問題