Ajax.BeginForm OnBegin confirmation Via jquery modal

后端 未结 3 418
误落风尘
误落风尘 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: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 }
    ))
    {
        
    }
    

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

提交回复
热议问题