2011-08-30 57 views
0
@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"方法。我可以給MVC Ajax回調一個參數嗎?

由於這個ajax鏈接在很多不同的拍賣中,每個拍賣都有自己的定時器,所以我需要能夠告訴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" }) 

不會被調用的警報消息。有任何想法嗎?

回答

1

你可以嘗試這樣的事情:

@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),例如使用字符串連接來設置參數:

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

UPDATE:

哦,對不起,看起來像per-也許它希望只是一個函數的名稱,作爲一個字符串,我認爲它是期待引用該函數。

您是否可以在現有代碼的同一時間在頁面上動態地包含其他腳本?如果是這樣,更改上面說OnSuccess="UpdateProxy",然後動態輸出如下:

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

如果你有在頁面上,同時你需要有UpdateProxy1(), UpdateProxy2()等多個Ajax鏈接。

+0

這是很好的建議,並在乍看之下,似乎這會工作,但不幸的是Update方法未在調用的回調。這是我的頁面上呈現的確切的HTML:'data-ajax-success =「function(){Update(3);}」' - 任何其他建議? –

+0

我已經用我能想到的唯一其他建議更新了我的答案。我之前沒有遇到過這個問題,因爲我沒有使用你正在使用的@ Ajax.ActionLink方法:我傾向於通過包含一些jQuery來完成我的ajax工作,這樣我就可以完全控制,而不必擔心什麼會或什麼不會爲我生成。 – nnnnnn

+0

工作就像一個魅力。丹科! –

0

你能嘗試

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

是的,這看起來好像會起作用,但Update方法在回調期間未被調用。即使在沒有參數連接的情況下調用Update(3),也不會調用該方法。有任何想法嗎? –

相關問題