Ajax.BeginForm OnBegin确认通过jquery模式

Sha*_*ean 4 javascript asp.net-mvc jquery 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="" />
}
Run Code Online (Sandbox Code Playgroud)

我想在发送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>
Run Code Online (Sandbox Code Playgroud)

如javascript代码中所示,对话框是异步打开的,这会导致该方法无法返回任何内容,并且无需用户选择是或否,表单就会被提交.我该如何解决?

Dar*_*rov 6

你可以使用普通Html.BeginForm和AJAXify与jquery.与Ajax.BeginForm帮助者相比,您将拥有更多的控制权:

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

然后在一个单独的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;
    });
});
Run Code Online (Sandbox Code Playgroud)