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 badges3 Answers
Reset to default 6You 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
版权声明:本文标题:javascript - Ajax.BeginForm OnBegin confirmation Via jquery modal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744016706a2576460.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论