简体   繁体   English

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

[英]ASP.NET MVC3 Ajax.ActionLink - Conditional confirmation dialog box

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). 我有一个@ Ajax.ActionLink,只有在满足某些条件(用户有未保存的更改)时才会显示确认对话框。 I created a javascript function that shows the confirmation dialog as needed, and returns true or false based on the response. 我创建了一个javascript函数,根据需要显示确认对话框,并根据响应返回true或false。 I tied it into the onclick event of the ActionLink but a false result does not cancel the action. 我将它绑定到ActionLink的onclick事件,但是错误的结果不会取消操作。 Here's a sample of my code: 这是我的代码示例:

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

Here's the javascript function 这是javascript函数

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

What's the best approach to display a conditional confirmation dialog box for the Ajax.ActionLink? 显示Ajax.ActionLink的条件确认对话框的最佳方法是什么?

Use the OnBegin event: 使用OnBegin事件:

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

You could also use the Confirm ajax option if all you need to do is pop up a confirm box. 如果您只需要弹出一个确认框,也可以使用Confirm ajax选项。 If you need to do more custom logic (or want to use a custom dialog) then you would need to use OnBegin. 如果您需要执行更多自定义逻辑(或想要使用自定义对话框),则需要使用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" 
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM