簡體   English   中英

我可以給MVC Ajax回調一個參數嗎?

[英]Can I give the MVC Ajax callback a parameter?

   @Ajax.ActionLink("Pujar",
                    "BidOnSmallAuction", 
                    "Auctions",
                    new { id = @item.UniqueIdentifierID },
                    new AjaxOptions { UpdateTargetId = "divright" + item.UniqueIdentifierID, InsertionMode = InsertionMode.Replace, OnSuccess = "Update" },
                    new { @class = "btn primary" })

例如,當我單擊該ActionLink時,將在回調之后調用javascript "Update"方法。

由於此ajax鏈接在許多不同的拍賣中,每個拍賣都有其自己的計時器,因此我需要能夠告訴Update方法運行哪個拍賣。

因此,如果您可以告訴我如何將Update方法傳遞給參數,那么我可以找出其余的參數。

謝謝你的時間。


編輯:

遵循兩個答案的建議,我嘗試運行以下命令:

//Just for testing purposes.
function Update(uniqueDivId) {
    alert(uniqueDivId);    
} 

//And in the view's code:

@Ajax.ActionLink("Pujar",
                 "BidOnSmallAuction",
                 "Auctions",
                 new { id = @item.UniqueIdentifierID },
                 new AjaxOptions { UpdateTargetId = "divright" + item.UniqueIdentifierID,               
                                   InsertionMode = InsertionMode.Replace, 
                                   OnSuccess = "function() { Update(3); }" },
                 new { @class = "btn primary" })

沒有調用警報消息。 有任何想法嗎?

您可以嘗試如下操作:

@Ajax.ActionLink(
   "Pujar",
   "BidOnSmallAuction",
   "Auctions",
   new { id = @item.UniqueIdentifierID },
   new AjaxOptions { UpdateTargetId = "divright" + item.UniqueIdentifierID,
                     InsertionMode = InsertionMode.Replace,
                     OnSuccess = "function() { Update(someParam); }" },
   new { @class = "btn primary" }) 

這樣OnSuccess本身會調用沒有參數的函數,但是該函數又會知道如何使用參數調用Update()。 您應該能夠使用字符串連接來設置所需的參數(類似於對UpdateTargetId所做的UpdateTargetId ),例如:

...
new AjaxOptions { UpdateTargetId = "divright" + item.UniqueIdentifierID,
    InsertionMode = InsertionMode.Replace,
    OnSuccess = "function() { Update(" + item.UniqueIdentifierID + "); }" },
...

更新:

哦,對不起,每當可能它只是期望函數的名稱為字符串,而我以為是期望對函數的引用。

您可以在現有代碼的同時動態在頁面上包括其他腳本嗎? 如果是這樣,請將上面的內容更改為說OnSuccess="UpdateProxy" ,然后動態輸出以下內容:

<script>
function UpdateProxy() {
   Update(/* insert your item.UniqueIdentifierID or other params here */);
}
</script>

如果頁面上同時有多個ajax鏈接,則需要具有UpdateProxy1(), UpdateProxy2()等。

你可以試試

OnSuccess = "function() { Update(\"some param\"); }"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM