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").dialog("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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With