Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 Ajax.ActionLink with JQuery UI Confirm Dialog

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" }
                    )
like image 600
Shuaib Avatar asked May 26 '11 03:05

Shuaib


3 Answers

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" })
like image 77
Hamid Tavakoli Avatar answered Dec 27 '22 19:12

Hamid Tavakoli


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"
                    })
like image 39
Shuaib Avatar answered Dec 27 '22 20:12

Shuaib


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.

like image 33
franco.cunza Avatar answered Dec 27 '22 19:12

franco.cunza