javascript ASP.NET MVC3 Ajax.ActionLink - 条件确认对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9604942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC3 Ajax.ActionLink - Conditional confirmation dialog box
提问by Alex
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,只有在满足某些条件(用户有未保存的更改)时,我才希望显示一个确认对话框。我创建了一个 javascript 函数,根据需要显示确认对话框,并根据响应返回 true 或 false。我将其绑定到 ActionLink 的 onclick 事件中,但错误的结果不会取消该操作。这是我的代码示例:
@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 显示条件确认对话框的最佳方法是什么?
回答by Dismissile
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. If you need to do more custom logic (or want to use a custom dialog) then you would need to use OnBegin.
如果您需要做的只是弹出一个确认框,您也可以使用 Confirm ajax 选项。如果您需要执行更多自定义逻辑(或想要使用自定义对话框),则需要使用 OnBegin。
Here is an example of using Confirm:
以下是使用 Confirm 的示例:
@Ajax.ActionLink("Done", "ActionName",
new AjaxOptions
{
Confirm= "Are you sure you want to do this?",
UpdateTargetId = "MyContainerId"
})