4

I have used a submit button to perform delete operation in mvc3. I want to show a confirmation box when I click the button so I used the below jquery. The query was used for static data but I want to work with my data base. When I click to the submit button the message appears but even if i click ok it shows error message. What should I do to make it work.

<script type="text/javascript">
    $(document).ready(function () {
               $("#button").click(function (event) {
            event.preventDefault();
            var link = this;
            if (confirm("Are you sure that you want to delete this user?")) {
                $.ajax({

                    type: "POST",
                    url: link.href,


                    success: function (data) 
                    {
                        $(link).parents("tr").remove();
                        alert("deleted");                     
                     },

                    error: function (data)
                     {
                        event.preventDefault();
                        alert(" Unsuccessful");
                      }
                });

            }
        }
            );
    });
Sanjay Maharjan
  • 671
  • 8
  • 24
  • http://stackoverflow.com/questions/887029/how-to-implement-confirmation-dialog-in-jquery-ui-dialog – Dr. Rajesh Rolen Jul 05 '12 at 12:26
  • my code does not seem to pass in success function and only shows unsuccessful. Once the code reaches in success I want to continue default action performed by the button. – Sanjay Maharjan Jul 06 '12 at 06:10

1 Answers1

1

Try this:

<asp:Button ID="btn" runat="server" Text="Click"
            OnClientClick="return confirmDialog(this);"
            onclick="btn_Click" />

var confirmed = false;
function confirmDialog(obj)
{
    if(!confirmed)
    {
        $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:140,
            modal: true,
            buttons: {
                "Yes": function()
                {
                    $( this ).dialog( "close" );
                    confirmed = true; obj.click();
                },
                "No": function()
                {
                    $( this ).dialog( "close" );
                }
            }
        });
    }

    return confirmed;
}

http://markmintoff.com/2011/03/asp-net-jquery-confirm-dialog/

How to implement "confirmation" dialog in Jquery UI dialog?

Community
  • 1
  • 1
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178
  • I tried to use the code but it was not supported by my jquery. which version will actually support this. also I am using mvc3 framework so it doesn't seem to use aspx functions. If you could explain it in jquery only it will be very helpful. thanks for the help. – Sanjay Maharjan Jul 06 '12 at 04:56