Ajax.BeginForm OnBegin confirmation Via jquery modal

后端 未结 3 388
误落风尘
误落风尘 2021-01-06 08:03

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

@using (Ajax.BeginForm(\"DeleteUser\", \"Administrator\", new { id = Model }, new AjaxOptions {         


        
相关标签:
3条回答
  • 2021-01-06 08:09

    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();
        });   
    
    0 讨论(0)
  • 2021-01-06 08:20

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

    0 讨论(0)
  • 2021-01-06 08:30

    You could use a normal Html.BeginForm and AJAXify it with jquery. You will have much more control compared 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;
        });
    });
    
    0 讨论(0)
提交回复
热议问题