2012-03-07 46 views
4

我有一個@Ajax.ActionLink,我只想在滿足某些條件(用戶有未保存的更改)時顯示確認對話框。我創建了一個javascript函數,根據需要顯示確認對話框,並根據響應返回true或false。我將它綁定到ActionLink的onclick事件中,但錯誤的結果不會取消操作。這裏是我的代碼示例:ASP.NET MVC3 Ajax.ActionLink - 有條件的確認對話框

@Ajax.ActionLink("Done", .. , .. , 
        new AjaxOptions() { UpdateTargetId = "MyContainerId"}, 
        new { onclick = "ConfirmDone()" }) 

這裏的javascript函數

function ConfirmDone() { 
    //for testing purposes we can always show the dialog box 
    return confirm("Are you sure you want to lose unsaved changes?"); 
} 

什麼,以顯示Ajax.ActionLink條件確認對話框,最好的辦法?

+0

我會用一個單獨的表格和按鈕如下所述: http://stackoverflow.com/a/30759201/869290 – fireydude 2015-06-11 09:46:29

回答

12

使用OnBegin事件:

@Ajax.ActionLink("Done", "ActionName", 
    new AjaxOptions 
    { 
     OnBegin = "return ConfirmDone()", 
     UpdateTargetId = "MyContainerId" 
    }) 

你也可以使用確認AJAX的選擇,如果你需要做的是流行打開一個確認框。如果你需要做更多的自定義邏輯(或者想使用自定義對話框),那麼你需要使用OnBegin。

下面是使用確認的示例:

@Ajax.ActionLink("Done", "ActionName", 
    new AjaxOptions 
    { 
     Confirm= "Are you sure you want to do this?", 
     UpdateTargetId = "MyContainerId" 
    }) 
+0

謝謝!我之前嘗試過OnBegin,但是我錯過了返回關鍵字 – Alex 2012-03-07 16:20:22

-1

我認爲你只需要更改如下:

@Ajax.ActionLink("Done", .. , .. , 
       new AjaxOptions() { UpdateTargetId = "MyContainerId"}, 
       new { onclick = "return ConfirmDone();" }) 
+0

幾乎右。你有正確的想法,我錯過了返回,但該功能需要在OnBegin部分 – Alex 2012-03-07 16:18:59

+0

-1執行,這是行不通的。 – Derrick 2012-10-23 20:07:53