'Confirm popup working fine but not delete the item in .net core

public async TaskOnPostDelete(int Id)
{

        var book= await _db.Book.FindAsync(Id);

        if(book == null) 
        {
            return NotFound();
        }
        _db.Book.Remove(book);
        await _db.SaveChangesAsync();
        return RedirectToPage("Index");
    }

Delete



Solution 1:[1]

You can involve your button in Form Tag :

<form asp-page-handler="Delete" method="post"  asp-route-id="3" onclick="return confirm('Are you Sure you want to delete ?')">
    <button  class="btn btn-danger btn-sm">Delete</button>
</form> 

And name your handler to OnPostDelete , and redirect to another page using RedirectToPage method :

public ActionResult OnPostDelete(int Id)
{          
    return RedirectToPage("Privacy", "Build",   new { id = Id });      
}

Solution 2:[2]

Use it instant off using

<button asp-page-handler="Delete" asp-route-id="@item.Id" onclick="return confirm('Are you Sure you want to delete ?');" class="btn btn-danger btn-sm">Delete</button>

use it

 <form asp-page-handler="Delete" method="post"  asp-route-id="@item.Id" onclick="return confirm('Are you Sure you want to delete ?')">
<button  class="btn btn-danger btn-sm">Delete</button>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nan Yu
Solution 2 mizanur rahman