12

在 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 来实现这一点?

4

4 回答 4

6

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

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

步骤:
1-从 AjaxOptions 中删除Confirm并避免设置按钮的 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。
我希望这有帮助。

于 2013-08-30T17:54:54.753 回答
4

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

于 2013-05-09T21:27:26.083 回答
2

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

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

注意:此示例使用Bootstrap 对话框

将 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);
    }
于 2016-11-08T08:32:05.573 回答
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
        }
    });
于 2019-06-06T16:02:55.937 回答