Delete button with Javascript in ASP.NET application

0

I have an application that saves in a database and has a button to delete, I already performed the delete function and ask for confirmation, but when the cancel button is given it also deletes the record. The function that I have is this:

<script>
    function confirmDel() {
        var agree = confirm("¿Realmente desea eliminarlo? ");
        if (agree) return true;
        return false;
    }
</script>

And I call it on the button like this:

<a onclick="confirmDel();" href="@Url.Action("Eliminar", "Agenda", new { id = contacto.Id })" class="btn btn-danger">Eliminar</a>

I hope to have your help.

    
asked by Sebastian Cano Lopez 30.11.2017 в 18:08
source

2 answers

1

Use the Event#preventDefault() method to avoid redirecting if cancel is pressed.

Here is an example:

function confirmDel(e) {
    var agree = confirm("¿Realmente deseas visitar este sitio?");
    
    if (agree == false) 
      e.preventDefault(); 
}
<a onclick="confirmDel(event)" href="https://es.stackoverflow.com">Visitar stackoverflow en español?</a>

Notice that the object event had to be sent as parameter in onclick . preventDefault() prevents the control from executing if default behavior.

    
answered by 30.11.2017 / 18:42
source
0
<a  href="@Url.Action("Eliminar", "Agenda", new {Id = contacto.Id }, new {@class="btn btn-danger",onclick="confirmDel();"})>Eliminar</a>

Try this

    
answered by 30.11.2017 в 18:14