I have been learning MVC 3 recently and I have found two solutions to deleting records from my list view. However I would like components of both of them, but my lack of knowledge in javascript is making this very difficult.
I have these two links for deleting:
@Html.ActionLink("Delete", "Delete",
new { id = item.ID }, new { @class = "delete-link" }) |
@Ajax.ActionLink("Delete Ajax", "Delete", "MyController",
new {id = item.ID},
new AjaxOptions {
HttpMethod = "POST",
OnBegin = "return ConfirmDone()",
OnSuccess = "deleteConfirmation"
})
The first one uses the following javascript to delete the record:
<script>
$(function () {
var deleteLinkObj;
// delete Link
$('.delete-link').click(function () {
deleteLinkObj = $(this);
$('#delete-dialog').dialog('open');
return false;
});
//definition of the delete dialog.
$('#delete-dialog').dialog({
autoOpen: false, width: 400, resizable: false, modal: true,
buttons: {
"Continue": function () {
$.post(deleteLinkObj[0].href, function (data)
{
var rowId = "#myTableItem-id-" + data.id;
$('.myTable').find(rowId).hide('slow');
});
$(this).dialog("close");
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
});
</script>
The second link uses this script function for confirmation:
<script>
function ConfirmDone() {
return confirm("Are you sure you want delete this item?");
}
</script>
Now both of these solutions work fine, however I prefer the coding of the second link, but I like the confirmation box that jquery-ui produces in the first link. So I would like to blend them together.
What I think I need to do is when the Ajax.ActionLink calles the ConfirmDone() then I need to show a jquery dialog as I do with the first link. However I am unsure how to produce this and allow this dialog to return a true or false depending on the button that is pressed.
Any help would be much appreciated.
Thanks very much.
After a few hours of trying things out I have came up with a solution:
My link changed to this:
Which calls this function with the
OnBeginoption: