ASP.NET MVC3 Ajax.ActionLink - 有条件的确认对话框有条件、对话框、NET、ASP

2023-09-10 19:35:15 作者:22.柠萌小仙女

我有一个@ Ajax.ActionLink这对于我想只显示,如果某些条件得到满足一个确认对话框(用户有未保存的更改)。我创建了一个JavaScript函数,显示需要确认对话框,并根据响应返回true或false。我把它系到ActionLink的的onclick事件,而是一个虚假的结果不取消该操作。下面是我的code的例子:

I have an @Ajax.ActionLink which for which I would like display a confirmation dialog box only if certain conditions are met (user has unsaved changes). I created a javascript function that shows the confirmation dialog as needed, and returns true or false based on the response. I tied it into the onclick event of the ActionLink but a false result does not cancel the action. Here's a sample of my code:

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

下面是javascript函数

Here's the javascript function

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?

What's the best approach to display a conditional confirmation dialog box for the Ajax.ActionLink?

推荐答案

使用的OnBegin事件:

Use the OnBegin event:

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

您也可以使用确认AJAX的选择,如果你需要做的是弹出一个确认框。如果你需要做更多的自定义逻辑(或希望使用自定义对话框),那么你就需要使用OnBegin。

You could also use the Confirm ajax option if all you need to do is pop up a confirm box. If you need to do more custom logic (or want to use a custom dialog) then you would need to use OnBegin.

下面是使用确认一个例子:

Here is an example of using Confirm:

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