简体   繁体   English

Ajax.ActionLink和确认对话框

[英]Ajax.ActionLink and confirm dialog

I have some problem with 我有一些问题

@Ajax.ActionLink

I want to show confirm dialog, and yes, i know that i can do just: 我想显示确认对话框,是的,我知道我可以做到:

@Ajax.ActionLink("Do it!", "Delete", new AjaxOptions(){ Confirm = "Are you sure?" });

but I want to have my own MyConfirm dialog 但我想拥有自己的MyConfirm对话框
I use alertify . 我使用alertify

so my code is: 所以我的代码是:

 @Ajax.ActionLink("Do it!", "Delete", new AjaxOptions(){  OnBegin="return MyConfirm();"})

my JavaScript function: 我的JavaScript函数:

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

But if I in my MyConfirm() function just return ' false ' Ajax request stop and my " Delete " action doesn't start (so it works how it should works). 但是,如果我在MyConfirm()函数只返回“ ”,Ajax请求停止,我的“ 删除 ”操作不启动(所以它的工作原理应该如何工作)。 but in my example function MyConfirm() show me my MyConfirm dialog but it also immediately retruns true and " Delete " action begins! 但在我的示例函数MyConfirm()中显示我的MyConfirm对话框,但它也会立即重新生成true并且“ Delete ”操作开始! How to deal with that? 怎么处理?

As per: Javascript Alertify with return from confirm 按照: Javascript Alertify返回确认

Alertify is a non blocking code and it returns before user makes a response. Alertify是一种非阻塞代码,它在用户做出响应之前返回。 Use fiddler or firebug to see the timeline of user's choice and ajax request. 使用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
 }

According to http://yassershaikh.com/how-to-use-onbegin-method-with-ajax-beginform/ returning false should cancel Ajax call. 根据http://yassershaikh.com/how-to-use-onbegin-method-with-ajax-beginform/返回false应该取消Ajax调用。 but your function does not return anything at the moment. 但是你的功能目前还没有返回任何东西。

So the answer by Nicholas is probably the only correct one. 所以尼古拉斯的答案可能是唯一正确的答案。

In response to your comment. 回应你的评论。 Assuming you know how to block execution of js (which is a horrible thing to do! and you should not!) this will do the trick for you: 假设你知道如何阻止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;
}

I haven't used alertify , but from the method signature, I presume that alertify.confirm returns immediately and runs the callback method when the user closes the popup some time later. 我没有使用alertify ,但是从方法签名,我假设alertify.confirm立即返回并在用户稍后关闭弹出窗口时运行回调方法。

This means that your MyConfirm method also returns immediately, and if it does not return false, the ajax call is started. 这意味着您的MyConfirm方法也会立即返回,如果它不返回false,则启动ajax调用。

You can fix this by always returning false from MyConfirm , and only making the ajax call in your alertify.confirm callback function: 您可以通过始终从MyConfirm返回false来修复此MyConfirm ,并且只在alertify.confirm回调函数中进行ajax调用:

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; 
 }

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

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