Search code examples
asp.net-mvcjqueryconfirmationasp.net-mvc-partialview

ASP.net & MVC : AJAX & update PartialView & confirmation message


I'm using ASP.Net MVC to make my app

I'm actually trying to delete an entry from my DB, first i must have a confirmation message and when the action is completed i want to refresh my PartialView

here's my code

          <%= Ajax.ActionLink(
            "Supprimer", 
            "Delete", 
            "Users",
            new { item.ID },
            new AjaxOptions {
              Confirm= "confirmMethod",
              UpdateTargetId = "usersListeID",
              OnSuccess = "success"
           }
              )%>

The problem is the confirm option is a simple ajax message, i want to use my own message (well structured because I used ajax & jQuery to make it)

function confirmMethod() {
$.msgBox({
    title: "Demande de confirmation",
    content: "Voulez-vous effectuer cette action?",
    type: "confirm",
    buttons: [{ value: "Oui" }, { value: "Non"}],
    success: function (result) {
        if (result == "Non") {
            abort();
        }
    }
});}

Solution

  • You could use a standard Html.ActionLink:

    <%= Ajax.ActionLink(
        "Supprimer", 
        "Delete", 
        "Users",
        new { item.ID },
        new { @class = "delete" }
    ) %>
    

    and then use jQuery to subscribe to the .click() event of tis anchor:

    $(function() {
        $('.delete').click(function() {
            var anchor = this;
            // Show the confirmation dialog
            $.msgBox({
                title: "Demande de confirmation",
                content: "Voulez-vous effectuer cette action?",
                type: "confirm",
                buttons: [{ value: "Oui" }, { value: "Non"}],
                success: function (result) {
                    if (result == "Oui") {
                        // send the AJAX request if the user selected "Oui":
                        $.ajax({
                            url: anchor.href,
                            type: 'POST',
                            success: function(data) {
                                // When the AJAX request succeeds update the 
                                // corresponding element in your DOM
                                $('#usersListeID').html(data);
                            }
                        });
                    }
                }
            });
    
            // Always cancel the default action of the link
            return false;
        });
    });