【问题标题】:Call custom confirm dialog in Ajax.Beginform in MVC3在 MVC3 中的 Ajax.Beginform 中调用自定义确认对话框
【发布时间】:2013-05-04 11:55:27
【问题描述】:

当使用 Ajax.Beginform 函数中的 AjaxOptions.Confirm 属性时,我希望能够使用自定义 jquery 对话框,或者至少能够将按钮的文本从 OK/Cancel 更改为其他内容。像这样:

<div>
    @using (Ajax.BeginForm("Function", "Controller", new { id = theId }, new AjaxOptions
        {
            HttpMethod = "POST",
            UpdateTargetId = "theForm",
            InsertionMode = InsertionMode.Replace,
            LoadingElementId = "iconGif",
            OnBegin = "OnBegin",
            OnFailure = "OnFailure",
            OnSuccess = "OnSuccess",
            Confirm = "Are you sure?" //TODO: Confirm with different dialog?
        }, new { id = "feedback-form" }))
    {
        //Some stuff
        <button onclick="derp()">Submit</button>
    }
</div>

有没有办法通过 AjaxOptions.Confirm 属性使用 Ajax.Beginform 来实现这一点?

【问题讨论】:

标签: jquery ajax asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

我在自定义 AjaxOptions 确认文本时遇到了这个问题,该文本的值在呈现 Ajax.Beginform 时尚未创建
例如:
Confirm="Are you sure you want to create Customer Id" + someValue + "?"

最后找到了一个解决方案:该方法是关于使用 JQuery 更改提交按钮的行为以提取值,运行我自己的确认对话框并在用户确认时提交 Ajax 表单。

步骤:
1- 从 AjaxOptions 中删除 Confirmavoid 设置按钮的 type="submit",可以是 type="button"

<div>
    @using (Ajax.BeginForm("Function", "Controller", new AjaxOptions
        {
            HttpMethod = "POST",
            UpdateTargetId = "theForm",
            InsertionMode = InsertionMode.Replace,
            LoadingElementId = "iconGif",
            OnBegin = "OnBegin",
            OnFailure = "OnFailure",
            OnSuccess = "OnSuccess"
            // Confirm option has been removed 
        }, new { id = "feedback-form" }))
    {
        //Some stuff
        <button id="buttonId" type="button" onclick="derp()">Submit</button> //Setting id and type
    }
</div>

2- 将以下内容添加到页面或布局中已引用的 js 文件中。

$("#buttonId").click(function () { 
if(confirm("Are you sure you want to create Id "+$("#CustomerId").val() + " ?"))
{
$("#feedback-form").submit(); // Submitting the form if user clicks OK
    }
    });

'CustomerId' 是页面中某些隐藏输入的 id。
我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    不,您无法使用 AjaxOptions 的 Confirm 属性来实现此目的。这只是使用了window.confirm javascript 方法,该方法不允许任何 UI 自定义并且依赖于浏览器。您必须自己实现此功能。例如,您可能想查看jQuery UI dialog plugin

    【讨论】:

    • 谢谢@DarinDimitrov 我会适当地使用 Jquery UI Diloag。无论如何都值得问:)
    【解决方案3】:

    我一直在寻找解决方案并来到这里。尽管达林断然否认解决方案的可能性,但他的回答实际上让我找到了解决方案。

    如果您可以在捆绑包中提供 jquery.unobtrusive-ajax.js 文件,那么您可以继续使用解决方案

    注意:此示例使用Bootstrap Dialog

    将jquery.unobtrusive-ajax.js中的asyncRequest(element, options)函数替换为这个

       function asyncRequest(element, options) {            
            var confirm = element.getAttribute("data-ajax-confirm");
            if (confirm) {
                BootstrapDialog.confirm({
                    title: 'Please Confirm!',
                    type: BootstrapDialog.TYPE_WARNING,
                    message: confirm,
                    draggable: true,
                    callback: function (result) {
                        if (result) {
                            NewMethod(element, options);
                        }                    
                    }
                });
            }
            else {
                NewMethod(element, options);
            }        
        }
    
        function NewMethod(element, options)
        {
            var loading, method, duration;
            loading = $(element.getAttribute("data-ajax-loading"));
            duration = parseInt(element.getAttribute("data-ajax-loading-duration"), 10) || 0;
    
            $.extend(options, {
                type: element.getAttribute("data-ajax-method") || undefined,
                url: element.getAttribute("data-ajax-url") || undefined,
                cache: !!element.getAttribute("data-ajax-cache"),
                beforeSend: function (xhr) {
                    var result;
                    asyncOnBeforeSend(xhr, method);
                    result = getFunction(element.getAttribute("data-ajax-begin"), ["xhr"]).apply(element, arguments);
                    if (result !== false) {
                        loading.show(duration);
                    }
                    return result;
                },
                complete: function () {
                    loading.hide(duration);
                    getFunction(element.getAttribute("data-ajax-complete"), ["xhr", "status"]).apply(element, arguments);
                },
                success: function (data, status, xhr) {
                    asyncOnSuccess(element, data, xhr.getResponseHeader("Content-Type") || "text/html");
                    getFunction(element.getAttribute("data-ajax-success"), ["data", "status", "xhr"]).apply(element, arguments);
                },
                error: function () {
                    getFunction(element.getAttribute("data-ajax-failure"), ["xhr", "status", "error"]).apply(element, arguments);
                }
            });
    
            options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" });
    
            method = options.type.toUpperCase();
            if (!isMethodProxySafe(method)) {
                options.type = "POST";
                options.data.push({ name: "X-HTTP-Method-Override", value: method });
            }
    
            $.ajax(options);
        }
    

    【讨论】:

    • 嗯...这是一个有趣的解决方案。我必须应用它进行测试。
    • 另请注意,这是在 MVC 5 的项目中完成的。
    【解决方案4】:

    您可以使用 sweetalert 添加这样的自定义警报

     $(".SubmitButton").click(function (evt) {
            $("#Form").validate();
            if ($("#Form").valid()) {
                evt.preventDefault();
                swal({
                    title: "Are you sure want to submit?",
                    text: "You will not be able to edit this item after submit",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonClass: "btn-danger",
                    confirmButtonText: "Confirm",
                    cancelButtonText: "Cancel",
                    closeOnConfirm: true,
                    closeOnCancel: true
                },
                    function (isConfirm) {
                        if (isConfirm) {
                           // swal("Deleted!", "Your item deleted.", "success");
    
                            $("#Form").submit()
                        }
                    });
            } else {
               // error message displays
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2010-12-20
      相关资源
      最近更新 更多