javascript - Ajax.BeginForm OnBegin 确认 Via jquery modal

标签 javascript jquery asp.net-mvc asp.net-mvc-3

我正在使用 jQuery UI 对话框。我有一个删除表单如下:

@using (Ajax.BeginForm("DeleteUser", "Administrator", new { id = Model }, new AjaxOptions { OnSuccess = "Deleted", OnBegin = "DeletingUser" }, new { id = "frm" + Model, name = Model }))
{
    <input type="submit" value="" />
}

我希望在发送 ajax 请求之前,弹出一个模式确认,用户选择是或否。

这是我的javascript:

<script>
function DeletingUser(){
    $( "#dialog-confirm" ).dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            "Delete all items": function() {
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });
            //I need to return a true or false here depending on the button clicked.
}
</script>



<div id="dialog-confirm" title="Empty the recycle bin?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>

如在 javascript 代码中所见,对话框是异步打开的,这导致该方法不返回任何内容,并且无论如何都会提交表单,而无需用户选择是或否。我该如何解决这个问题?

最佳答案

您可以使用普通的 Html.BeginForm 并使用 jquery 对其进行 AJAX 化。与 Ajax.BeginForm 助手相比,您将拥有更多的控制权:

@using (Html.BeginForm(
    "DeleteUser", 
    "Administrator", 
    new { id = Model }, 
    FormMethod.Post, 
    new { id = "frm" + Model, name = Model }
))
{
    <input type="submit" value="" />
}

然后在一个单独的 javascript 文件中简单地:

$(function() {
    $('form[id^="frm"]').submit(function() {
        var $form = $(this);
        $('#dialog-confirm').dialog({
            resizable: false,
            height:140,
            modal: true,
            buttons: {
                'Delete all items': function() {
                    $(this).dialog('close');
                    // the user confirmed => we send an AJAX request to delete
                    $.ajax({
                        url: $form.attr('action'),
                        type: $form.attr('method'),
                        data: $form.serialize(),
                        success: function(result) {
                            Deleted(result);
                        }
                    });
                },
                Cancel: function() {
                    $(this).dialog('close');
                }
            }
        }
        return false;
    });
});

关于javascript - Ajax.BeginForm OnBegin 确认 Via jquery modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5572573/

相关文章:

javascript - 是否可以在脚本位置可靠地插入 HTML 元素?

javascript - window.status 不起作用

c# - 使用带有 MVC4 的高级集线器 api 的 SignalR 错误 - "/echo/negotiate 404 Not Found Error"

javascript - 将数组的数组转换为键值对的数组

javascript - Silverstripe 图片上传为 javascript 变量

c# - 如何处理 ASP MVC 中的重复表单字段

javascript - pdf渲染时显示加载图像

javascript - 使用 AngularJS 的 Windows Phone 8.1 上的 Phonegap 无法从我的 API 检索 JSONP

javascript - AngularJs : json parsing

javascript - 使用 javascript 将 css 传递到新页面不起作用