Ajax.BeginForm OnBegin确认通过jquery模式

时间:2011-04-06 20:31:39

标签: 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代码中所示,对话框是异步打开的,这会导致该方法无法返回任何内容,并且无需用户选择是或否,表单就会被提交。我该如何解决这个问题?

3 个答案:

答案 0 :(得分:6)

您可以使用普通Html.BeginForm并使用jquery对其进行AJAXify。与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;
    });
});

答案 1 :(得分:0)

我认为该表单已提交,因为您的DeletingUser在发布时间不可用。看到这个stackoverflow article(还没有解决方案)

答案 2 :(得分:0)

一招。这是我的第一篇文章,我花了很多时间来解决这个问题,我认为这更容易:

 dialog:

function smOpenConfirmDialog(callBack) {
$("#noticeDialog").dialog({
    autoOpen: true,
    modal: true,
    autoOpen: false,
    draggable: false,
    buttons: {
        "Confirm": function () {
            callBack();
            $(this).dialog("close");
        },
        "Cancel": function () {
            $(this).dialog("close");
        }
    }
});

   $("input:submit").click(function (e) {
        smOpenConfirmDialog(function () {
            $("form").trigger('submit');
        });
        e.preventDefault();
    });