在MVC3中的Ajax.Beginform中调用自定义确认对话框

时间:2013-05-09 21:25:03

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

我希望能够使用自定义jquery对话框,或者至少能够在Ajax.Beginform函数中使用AjaxOptions.Confirm属性时将按钮的文本从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实现这一点?

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:3)

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

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

步骤:
1-从AjaxOptions中删除确认避免设置按钮的类型=“提交”,可以是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 :(得分:2)

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

如果您可以在捆绑包中提供 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);
    }

答案 3 :(得分:0)

您可以使用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
        }
    });