Ajax.BeginForm OnBegin确认通过jquery模式

我正在使用jQuery UI对话框。 我有一个删除表格如下:

@using (Ajax.BeginForm("DeleteUser", "Administrator", new { id = Model }, new AjaxOptions { OnSuccess = "Deleted", OnBegin = "DeletingUser" }, new { id = "frm" + Model, name = Model })) {  } 

我想在发送ajax请求之前弹出模态确认,用户选择是或否。

这是我的javascript:

  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. }  

These items will be permanently deleted and cannot be recovered. Are you sure?

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

您可以使用普通的Html.BeginForm并使用jquery对其进行AJAXify。 与Ajax.BeginForm帮助器相比,您将拥有更多控制权:

 @using (Html.BeginForm( "DeleteUser", "Administrator", new { id = Model }, FormMethod.Post, new { id = "frm" + Model, name = Model } )) {  } 

然后在一个单独的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; }); }); 

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

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

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