Ajax.ActionLink和确认对话框 [英] Ajax.ActionLink and confirm dialog

查看:316
本文介绍了Ajax.ActionLink和确认对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,

@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?" });

但我想有我自己的MyConfirm对话框
我使用 alertify

but I want to have my own MyConfirm dialog
I use alertify.

让我的code是:

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

我的JavaScript函数:

my JavaScript function:

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

但是,如果我在我的 MyConfirm()函数只是返回'的错误Ajax请求停止,我的删除动作不启动(所以它的工作原理应该如何工作)。但在我的例子功能的 MyConfirm()显示了我MyConfirm对话框,但它也立即retruns真实,删除的行动开始了!如何处理呢?

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). but in my example function MyConfirm() show me my MyConfirm dialog but it also immediately retruns true and "Delete" action begins! How to deal with that?

推荐答案

按<一href="http://stackoverflow.com/questions/14408627/javascript-alertify-with-return-from-confirm">Javascript Alertify与确认回报

Alertify是不可阻挡code和返回用户做出响应之前。使用小提琴手或萤火看用户的选择和Ajax请求的时间表。

Alertify is a non blocking code and it returns before user makes a response. Use fiddler or firebug to see the timeline of user's choice and ajax request.

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
 }

根据 http://yassershaikh.com/how -to-使用-onbegin法与 - AJAX-beginform / 返回false应该取消Ajax调用。但你的功能不此刻返回任何东西。

According to http://yassershaikh.com/how-to-use-onbegin-method-with-ajax-beginform/ returning false should cancel Ajax call. but your function does not return anything at the moment.

于是尼古拉斯答案很可能是唯一正确的。

So the answer by Nicholas is probably the only correct one.

在回应您的评论。假设你知道如何阻止执行的JS这会做的伎俩,你(这是一个可怕的事情,你不应该!):

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:

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

这篇关于Ajax.ActionLink和确认对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆