Hi @打玻璃
But if I do not use confirm and I use sweetalert2 to show confirm
If you are using sweetalert2, you could use event.preventDefault()
to prevent the default navigate behavior, then based on the confirm result to call the handler method, refer to the following sample:
<a class="btn btn-primary" asp-page="/Privacy" asp-page-handler="Delete" onclick="is_delete(this)">Delete (Get)</a>
@section Scripts{
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/5.0.7/sweetalert2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/5.0.7/sweetalert2.min.js"></script>
<script>
function is_delete(e) {
event.preventDefault(); //prevent the default navigate behavior.
var url = $(e).attr("href"); //get the hyperlink href attribute.
swal({
title: 'Are you sure to delete it?',
text: 'Once deleted, you will not be able to recover this imaginary file!',
icon: 'warning',
buttons: ['cancel', 'delete'],
showCancelButton: true,
dangerMode: true
}).then((willDelete) => {
console.log(willDelete);
//if use click Ok,
if(willDelete){
//use windows.location.href to call the handler method.
window.location.href = url;
//If you want to call the OnPost handler method, try to use the following code to submit the form.
//$("#myform").submit();
}
});
}
</script>
}
The result as below:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion