admin管理员组

文章数量:1356908

I'm using jQuery UI dialog. I have a delete form as follows:

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

I want before the ajax request is sent, a modal confirmation pops up and the user selects a yes or no.

Here is my 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>

As seen in the javascript code, the dialog is opened asynchronously, which causes the method to return nothing and the form getting submitted anyways without the user selecting a yes or no. How do I fix this?

I'm using jQuery UI dialog. I have a delete form as follows:

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

I want before the ajax request is sent, a modal confirmation pops up and the user selects a yes or no.

Here is my 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>

As seen in the javascript code, the dialog is opened asynchronously, which causes the method to return nothing and the form getting submitted anyways without the user selecting a yes or no. How do I fix this?

Share Improve this question asked Apr 6, 2011 at 20:31 Shawn McleanShawn Mclean 57.5k96 gold badges281 silver badges412 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You could use a normal Html.BeginForm and AJAXify it with jquery. You will have much more control pared to an Ajax.BeginForm helper:

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

and then in a separate javascript file simply:

$(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;
    });
});

I think the form is getting submitted because your DeletingUser is not available at post time. see this stackoverflow article (no solution yet)

A trick. This is my first post, i spend a lot of time to resolve this question, i think that this is easier:

 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");
        }
    }
});

and

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

本文标签: javascriptAjaxBeginForm OnBegin confirmation Via jquery modalStack Overflow