Search code examples
c#razorblazor

Blazor - show confirmation dialog before delete/update?


In the following Blazor (server-side) code snips. How to prompt the confirmation dialog?

<tbody>
    @foreach (var r in lists)
    {
        var s = r.ID;
        <tr>
            <td>@s</td>
            <td><button class="btn btn-primary" @onclick="() => DeleteSymbol(s)">Delete</button></td>
        </tr>
    }
</tbody>

@code {
    async Task DeleteSymbol(string id)
    {
        // Get confirmation response from user before running deletion?
        // Delete!
    }
}

Solution

  • @inject IJSRuntime JsRuntime
    
    <tbody>
    ...
    </tbody>
    
    @code {
        async Task DeleteSymbol(string id)
        {
            bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Are you sure?");
            if (confirmed)
            {
                // Delete!
            }
        }
    }