In a Blazor application, you can show a confirmation dialog before deleting or updating an item by using the JSRuntime class to invoke a JavaScript function that displays the dialog.
Here's an example of how to show a confirmation dialog in Blazor:
In your Blazor component, add a method to show the confirmation dialog:
private async Task<bool> ShowConfirmationDialogAsync(string message) { return await JSRuntime.InvokeAsync<bool>("confirm", message); } This method uses the JSRuntime.InvokeAsync method to call the confirm JavaScript function, which displays a dialog box with the specified message and returns a Boolean value indicating whether the user clicked OK or Cancel.
In your Blazor component, call the ShowConfirmationDialogAsync method before deleting or updating an item:
private async Task DeleteItemAsync(Item item) { if (await ShowConfirmationDialogAsync("Are you sure you want to delete this item?")) { // Delete the item } } This code calls the ShowConfirmationDialogAsync method to display a confirmation dialog with the specified message. If the user clicks OK, the code proceeds with deleting the item. If the user clicks Cancel, the code does not delete the item.
You can use a similar approach to show a confirmation dialog before updating an item. Simply call the ShowConfirmationDialogAsync method before updating the item, and proceed with the update if the user clicks OK.
Note that the JSRuntime class is used to invoke JavaScript functions in Blazor, so you need to inject it into your component before you can use it. You can inject JSRuntime by adding the following code to your component:
@inject IJSRuntime JSRuntime
This injects an instance of IJSRuntime into your component, which you can then use to invoke JavaScript functions.
"Blazor confirmation dialog before delete"
<button @onclick="DeleteItem">Delete</button> @code { private async Task DeleteItem() { if (await JSRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to delete?")) { // Perform delete logic Console.WriteLine("Item deleted!"); } } JSRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to delete?")) to display a confirmation dialog before executing the delete logic."Blazor confirmation dialog before update"
<button @onclick="UpdateItem">Update</button> @code { private async Task UpdateItem() { if (await JSRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to update?")) { // Perform update logic Console.WriteLine("Item updated!"); } } "Blazor confirm dialog before form submission"
<form @onsubmit="SubmitForm"> <!-- Form fields go here --> <button type="submit">Submit</button> </form> @code { private async Task SubmitForm() { if (!await JSRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to submit the form?")) { // Cancel form submission return; } // Perform form submission logic Console.WriteLine("Form submitted!"); } "Blazor show confirmation dialog on row delete"
@foreach (var item in items) { <div> <span>@item.Name</span> <button @onclick="() => DeleteItem(item)">Delete</button> </div> } @code { private async Task DeleteItem(ItemModel item) { if (await JSRuntime.InvokeAsync<bool>("confirm", $"Are you sure you want to delete {item.Name}?")) { // Perform delete logic Console.WriteLine($"{item.Name} deleted!"); } } "Blazor confirmation modal before delete"
<button @onclick="ShowDeleteConfirmation">Delete</button> <ConfirmationModal @ref="deleteConfirmationModal" /> @code { private ConfirmationModal deleteConfirmationModal; private async Task ShowDeleteConfirmation() { if (await deleteConfirmationModal.Show("Are you sure you want to delete?")) { // Perform delete logic Console.WriteLine("Item deleted!"); } } "Blazor confirmation dialog with custom messages"
<button @onclick="ShowCustomConfirmation">Delete</button> @code { private async Task ShowCustomConfirmation() { string customMessage = "This action is irreversible. Do you really want to proceed?"; if (await JSRuntime.InvokeAsync<bool>("confirm", customMessage)) { // Perform delete logic Console.WriteLine("Item deleted!"); } } "Blazor confirmation dialog with parameters"
<button @onclick="() => ShowConfirmation("Are you sure you want to delete?", DeleteItem)">Delete</button> @code { private async Task ShowConfirmation(string message, Func<Task> action) { if (await JSRuntime.InvokeAsync<bool>("confirm", message)) { await action.Invoke(); } } private async Task DeleteItem() { // Perform delete logic Console.WriteLine("Item deleted!"); } "Blazor confirmation dialog with callback"
<button @onclick="() => ShowConfirmation("Are you sure you want to delete?", DeleteItem)">Delete</button> @code { private async Task ShowConfirmation(string message, Func<Task> callback) { if (await JSRuntime.InvokeAsync<bool>("confirm", message)) { await callback.Invoke(); } } private async Task DeleteItem() { // Perform delete logic Console.WriteLine("Item deleted!"); } "Blazor confirmation dialog before navigation"
<NavLink @onclick="NavigateWithConfirmation" href="/destination">Go to Destination</NavLink> @code { private async Task NavigateWithConfirmation(MouseEventArgs e) { if (!await JSRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to navigate to the destination?")) { e.PreventDefault(); } } "Blazor confirmation dialog with multiple buttons"
<button @onclick="() => ShowConfirmation("Choose an action", new List<string> { "Delete", "Archive" }, HandleAction)">Perform Action</button> @code { private async Task ShowConfirmation(string message, List<string> options, Func<string, Task> action) { string selectedOption = await JSRuntime.InvokeAsync<string>("showCustomConfirm", message, options); if (!string.IsNullOrEmpty(selectedOption)) { await action.Invoke(selectedOption); } } private async Task HandleAction(string selectedOption) { // Perform action based on the selected option Console.WriteLine($"{selectedOption} action performed!"); } language-theory working-directory union exponent heap-memory angular2-router3 angular-validator maven-nar-plugin servlet-3.0 twitter-bootstrap-3