How to confirm then delete in razor pages?

mc 4,636 Reputation points
2023-01-18T00:51:40.3966667+00:00

I am using razor pages.

in the table there is button of delete I want to click it then confirm alert if the user click yes then delete the data.

<a href="btn btn-primary" asp-page-handler="Delete" >Delete</a>

how to do it?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,618 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,451 Reputation points Microsoft Vendor
    2023-01-18T05:44:32.96+00:00

    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:

    image3


    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

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,451 Reputation points Microsoft Vendor
    2023-01-18T01:26:00.1133333+00:00

    Hi @打玻璃

    in the table there is button of delete I want to click it then confirm alert if the user click yes then delete the data. <a href="btn btn-primary" asp-page-handler="Delete" >Delete</a>

    You can use the confirm() function in an inline onclick handler:

    <a class="btn btn-primary" asp-page="/Privacy" asp-page-handler="Delete" onclick="return confirm('Are you sure?')">Delete</a>
    

    [Note] using the above method, the Delete handler should be a OnGet method.

    If you want to use Post method, the code like this:

        <form asp-page-handler="Delete" method="post">         
          <button class="btn btn-default" onclick="return confirm('Are you sure?')">Delete</button>     
        </form>
    

    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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.