C# ActionLink和确认对话框

C# ActionLink和确认对话框,c#,javascript,asp.net,ajax,asp.net-mvc-4,C#,Javascript,Asp.net,Ajax,Asp.net Mvc 4,我有一些问题 @Ajax.ActionLink 我想显示确认对话框,是的,我知道我可以做到: @Ajax.ActionLink("Do it!", "Delete", new AjaxOptions(){ Confirm = "Are you sure?" }); 但我想拥有自己的MyConfig对话框 我使用警报 所以我的代码是: @Ajax.ActionLink("Do it!", "Delete", new AjaxOptions(){ OnBegin="return MyConf

我有一些问题

@Ajax.ActionLink
我想显示确认对话框,是的,我知道我可以做到:

@Ajax.ActionLink("Do it!", "Delete", new AjaxOptions(){ Confirm = "Are you sure?" });
但我想拥有自己的MyConfig对话框
我使用警报

所以我的代码是:

 @Ajax.ActionLink("Do it!", "Delete", new AjaxOptions(){  OnBegin="return MyConfirm();"})
我的JavaScript函数:

function MyConfirm() {
        alertify.confirm("Do you really want to do that??", function (e) {
           if (e) return true;
           else return false;
        });    
 }
function MyConfirm() {
    alertify.confirm("Do you really want to do that??", function (e) {

       // this actually makes the ajax call if required
       if (e) doAjaxCall();
    });    


    return false; 
 }

但是,如果我在我的MyConfirm()函数中只返回“false”Ajax请求停止,我的“Delete”操作不会启动(因此它应该如何工作)。但是在我的示例函数中,MyConfirm()显示我的MyConfirm对话框,但它也会立即重新运行true,并且“Delete”操作开始!如何处理?

我没有使用
alertify
,但是从方法签名来看,我假设
alertify.confirm
会立即返回,并在用户稍后关闭弹出窗口时运行回调方法

这意味着您的
MyConfirm
方法也会立即返回,如果它不返回false,则会启动ajax调用

您可以通过始终从
MyConfirm
返回
false
,并仅在
alertify中进行ajax调用来修复此问题。确认
回调函数:

function MyConfirm() {
        alertify.confirm("Do you really want to do that??", function (e) {
           if (e) return true;
           else return false;
        });    
 }
function MyConfirm() {
    alertify.confirm("Do you really want to do that??", function (e) {

       // this actually makes the ajax call if required
       if (e) doAjaxCall();
    });    


    return false; 
 }
根据:

Alertify是一个非阻塞代码,它在用户做出响应之前返回。使用fiddler或firebug查看用户选择和ajax请求的时间线

function MyConfirm() {
        alertify.confirm("Do you really want to do that??", function (e) {
           if (e) alert('after they pressed confirm, but ajax is already sent');
           else alert('after they pressed confirm, but ajax is already sent');
        });
        // no return here
 }
根据返回的false,应该取消Ajax调用。但是您的函数目前没有返回任何内容

所以尼古拉斯的答案可能是唯一正确的

回应你的评论。假设您知道如何阻止js的执行(这是一件可怕的事情!您不应该这样做!),这将为您带来好处:

// this tells us if use made a choice
var userClicked = false;
// this is for user's choice
var userChoice;

function MyConfirm() {
    alertify.confirm("Do you really want to do that??", function (e) {
        // mark that user clicked
        userClicked = true;
        if (e) {
            userChoice = true;
        } else {
            userChoice = false;
        }
    });

    // Put your delay code here 
    // userClicked tells you that user made a choice
    // Remember that setTimout will fail here as it's a fork, not a blocking function
    // you will have to do some crazy while loops that will make unicorns cry

    userClicked = false;
    return userChoice;
}

不管怎样,我没有时间。我想我只要按一下按钮就行了,它肯定能用……我知道!我尝试了所有的方法,然后才提出关于stackoverflow的问题。。。不过还是要谢谢你。。看看我的代码。你觉得我的和你的有什么不同吗?除了警报?阿贾克斯总是被阿莱拉迪发送的。。不管我做什么。。我甚至尝试了一些时间延迟,但它也不起作用