I have a @Ajax.ActionLink which calls a Delete Action. Now I want to use JQuery UI Dialog confirm instead the regular "Confirm" attribute of the Ajax link. I hook the event to the Ajax.ActionLink using the unobtrusive javaScript. But the action gets submitted and the e.preventDefault(); throughs an error. Can anyone tell me why this happens?
Here is my jQuery code:
$("[data-dialog-confirm]").click(function (e) {
e.preventDefault();
var theHREF = $(this).attr("href");
$("#dialog-confirm").dialog('option', 'buttons', {
"Delete this item": function () {
window.location.href = theHREF;
$(this).dialog("close");
},
"Cancel": function () {
$(this).dialog("close");
}
});
$("#dialog-confirm").di开发者_StackOverflow中文版alog("open");
});
Here is my MVC code:
@Ajax.ActionLink("Delete", "DeleteConfirmed", new { id = item.Id },
new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "refreshList"
},
new {data_dialog_confirm="true" }
)
Here is how I have implemented the confirm functionality with jquery UI:
$(document).ready( function () {
$("#dialog-confirm").dialog({
autoOpen: false,
modal: true,
resizable: false,
height:180,
});
$(".deleteLink").click(function(e) {
e.preventDefault();
var targetUrl = $(this).attr("href");
$("#dialog-confirm").dialog({
buttons : {
"Confirm" : function() {
window.location.href = targetUrl;
},
"Cancel" : function() {
$(this).dialog("close");
}
}
});
$("#dialog-confirm").dialog("open");
});
} );
and in your html you can add the dialog div
<div id="dialog-confirm" title="Delete the item?" >
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This item will be deleted. Are you sure?</p>
</div>
your action link should look like this
@Html.ActionLink("Delete", "UpdateDelete", new { id = item.Id }, new { @class = "deleteLink" })
I ended up doing this:
Change the Ajax.ActionLink
to Html.ActionLink
and in my JavaScript code I call $.get(theHREF, null, function () { refreshList() });
Here is the code:
$("#dialog-confirm").dialog({
autoOpen: false,
resizable: false,
width: 500,
modal: true,
buttons: {
"Delete this item": function () {
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
}
});
$("[data-dialog-confirm]").click(function (e) {
e.preventDefault();
var theHREF = $(this).attr("href");
$("#dialog-confirm").dialog('option', 'buttons', { "Yes":
function () {
$.get(theHREF, null, function () { refreshList() });
$(this).dialog("close");
}, "No":
function () { $(this).dialog("close"); }
});
$("#dialog-confirm").dialog("open");
});
Here is the MVC 3 ActionLink
@Html.ActionLink("Delete", "DeleteConfirmed", "Category", new { id = item.Id }, new
{
data_dialog_confirm = "true"
})
You migth use the OnBegin
property instead OnSuccess
, this is just a simple example but it could help you, this is how I declare my Ajax.ActionLink
link:
@Ajax.ActionLink("Delete", "DeletePeriod", "ConfigPeriod",
new { area = "Budget", id = Model.Id }, new AjaxOptions
{
HttpMethod = "Post",
OnBegin = "confirmDeletion"
})
And this could be a really simple implementation of the confirmDeletion
function:
<script>
function confirmDeletion (e) {
// Do something or validate inputs...
e.preventDefault(); // This should prevent the event to fire...
};
</script>
Regards.
Try this instead:
$("[data-dialog-confirm]").click(function (e) {
var theHREF = $(this).attr("href");
$("#dialog-confirm").dialog('option', 'buttons', {
"Delete this item": function () {
window.location.href = theHREF;
$(this).dialog("close");
},
"Cancel": function () {
$(this).dialog("close");
}
});
$("#dialog-confirm").dialog("open");
return false
});
I'd also strongly urge you to consider using the HttpDelete verb with any MVC postback or Ajax method.
Maybe you can try calling a confirmation function from the OnBegin
event of Ajax.ActionLink? That way you can keep using the Ajax.ActionLink. The OnBegin even is marked up as data-ajax-begin
attribute so have jquery assign "return YourConfirmationFunction()" to this attribute and you should be fine. Here's an example of using OnBegin for confirmation dialog without jquery.
I found that it was much easier to do this using a separate form, containing the post data, and a button which shows the dialog and then submits the form.
cshtml (Razor):
using (Ajax.BeginForm("Deactivate", "Admin",
new AjaxOptions { OnComplete = "ReloadList();" },
new {@id = "deactive-form-" + user.Id }))
{
<input type="hidden" name="id" value="@user.Id" />
}
<input type="button" class="btn" value="Deactivate"
onclick="return ShowConfirmDlgThenSubmitForm('dialog-deactivate', 'deactive-form-@user.Id');" />
javascripts (with jQuery UI Dialog):
function ShowConfirmDlgThenSubmitForm(dialogId, formId) {
$('#' + dialogId).dialog({
resizable: false,
height: 180,
modal: true,
buttons: {
"Okay": function () {
$(this).dialog("close");
$('#' + formId).submit();
},
Cancel: function () {
$(this).dialog("close");
}
}
});
return false;
}
精彩评论