MVC 3 Ajax.ActionLink with JQuery UI Confirm Dialog

前端 未结 6 1383
攒了一身酷
攒了一身酷 2020-12-29 15:55

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 eve

相关标签:
6条回答
  • 2020-12-29 16:16

    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;
        }
    
    0 讨论(0)
  • 2020-12-29 16:34

    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" })
    
    0 讨论(0)
  • 2020-12-29 16:39

    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"
                        })
    
    0 讨论(0)
  • 2020-12-29 16:39

    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.

    0 讨论(0)
  • 2020-12-29 16:39

    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.

    0 讨论(0)
  • 2020-12-29 16:39

    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.

    0 讨论(0)
提交回复
热议问题