Getting Started with Predefined Dialogs in Blazor

4 Nov 20257 minutes to read

This section explains how to include predefined dialogs in a Blazor WebAssembly app using Visual Studio and Visual Studio Code.

Prerequisites

Create a new Blazor App in Visual Studio

Create a Blazor WebAssembly App using Visual Studio via Microsoft templates or the Syncfusion® Blazor Extension.

Install Syncfusion® Blazor Popups and Themes NuGet in the App

To add the Blazor predefined dialog component, open the NuGet Package Manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), then search for and install Syncfusion.Blazor.Popups and Syncfusion.Blazor.Themes. Alternatively, use the following Package Manager commands:

Install-Package Syncfusion.Blazor.Popups -Version 31.2.12
Install-Package Syncfusion.Blazor.Themes -Version 31.2.12

NOTE

Syncfusion® Blazor components are available on NuGet. Refer to the NuGet packages topic for the list of available packages and component details. A valid Syncfusion license must be registered in the application. For guidance, see the licensing overview in the Syncfusion documentation.

Prerequisites

Create a new Blazor App in Visual Studio Code

Create a Blazor WebAssembly App using Visual Studio Code via Microsoft templates or the Syncfusion® Blazor Extension.

Alternatively, create a WebAssembly application using the following commands in the terminal(Ctrl+`).

dotnet new blazorwasm -o BlazorApp
cd BlazorApp

Install Syncfusion® Blazor Popups and Themes NuGet in the App

  • Press Ctrl+` to open the integrated terminal in Visual Studio Code.
  • Ensure the terminal is in the project root directory where the .csproj file is located.
  • Run the following commands to install the Syncfusion.Blazor.Popups and Syncfusion.Blazor.Themes packages and restore dependencies.
dotnet add package Syncfusion.Blazor.Popups -v 31.2.12
dotnet add package Syncfusion.Blazor.Themes -v 31.2.12
dotnet restore

NOTE

Syncfusion® Blazor components are available on NuGet. Refer to the NuGet packages topic for the list of available packages and component details. A valid Syncfusion license must be registered in the application. For guidance, see the licensing overview in the Syncfusion documentation.

Register Syncfusion® Blazor Service and Syncfusion® Dialog Service

Open ~/_Imports.razor and import the Syncfusion.Blazor and Syncfusion.Blazor.Popups namespaces.

@using Syncfusion.Blazor
@using Syncfusion.Blazor.Popups

Now, register the Syncfusion® Blazor service in the ~/Program.cs file of the Blazor WebAssembly app.

using Syncfusion.Blazor
using Syncfusion.Blazor.Popups

// Add services to the container.

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped<SfDialogService>();
builder.Services.AddSyncfusionBlazor();
await builder.Build().RunAsync();
....

Add stylesheet and script resources

The theme stylesheet and script are provided by NuGet through Static Web Assets. Include the stylesheet and script references in the <head> section of ~/wwwroot/index.html.

<head>
    ....
    <link href="_content/Syncfusion.Blazor.Themes/bootstrap5.css" rel="stylesheet" />
    <script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>
</head>

NOTE

See Blazor Themes to explore different methods (Static Web Assets, CDN, and CRG) for referencing themes in a Blazor application. Also, see Adding Script Reference for approaches to include script references.

Add Blazor Dialog Provider

SfDialogProvider opens predefined dialogs based on SfDialogService settings from anywhere in the application. Add SfDialogProvider in MainLayout.razor or in a specific page, but include it only once in the app. When added to MainLayout.razor, predefined dialogs can be opened from anywhere in the application. When added to a specific page, dialogs can be opened only within that page.

  • Now, add SfDialogProvider in the ~/_MainLayout.razor file.
<Syncfusion.Blazor.Popups.SfDialogProvider/>

Open Predefined Dialog

After adding SfDialogService and SfDialogProvider, open predefined dialogs from anywhere in the application using AlertAsync, ConfirmAsync, or PromptAsync methods in SfDialogService.

Show alert dialog

An alert dialog is used to display errors, warnings, or informational messages that require user acknowledgment. This is achieved using the DialogService.AlertAsync method. The alert dialog displays an OK button. When the user selects OK, the alert dialog closes.

In the following example, an alert dialog is displayed when a Syncfusion® Blazor Button is clicked.

@using Syncfusion.Blazor.Popups
@using Syncfusion.Blazor.Buttons
@inject SfDialogService DialogService
    <div>
        <SfButton @onclick="@AlertBtn">Alert</SfButton>
    </div>
    <div class="status" style ="padding-top:10px">@DialogStatus</div>
@code {
    private string DialogStatus { get; set; }
    private async Task AlertBtn()
    {
        await DialogService.AlertAsync("10% of battery remaining", "Low Battery");
        this.DialogStatus = "The user closed the Alert dialog";
    }
}
Alert Dialog

Show confirm dialog

A confirm dialog displays a specified message along with OK and Cancel buttons and returns a boolean value based on the user action. Selecting OK returns true; selecting Cancel returns false. This can be achieved using the DialogService.ConfirmAsync method. It is typically used to get user approval before a critical action. After the user responds, the dialog closes automatically.

In the following example, the confirm dialog is displayed and returns a value based on the OK or Cancel button click.

@using Syncfusion.Blazor.Popups
@using Syncfusion.Blazor.Buttons
@inject SfDialogService DialogService
    <div>
        <SfButton @onclick="@ConfirmBtn">Confirm</SfButton>
    </div>
    <div class="status" style ="padding-top:10px">
    @DialogStatus
</div>
@code {
    private string DialogStatus { get; set; }
    private async Task ConfirmBtn()
    {
        bool isConfirm = await DialogService.ConfirmAsync("Are you sure you want to permanently delete these items?", "Delete Multiple Items");
        string confirmMessage = isConfirm ? "confirmed" : "canceled";
        this.DialogStatus = $"The user {confirmMessage} the dialog box."; 
    }
}
Confirm Dialog

Show prompt dialog

A prompt dialog is used to get input from the user using the DialogService.PromptAsync method. Selecting OK returns the input value; selecting Cancel returns null. After the user responds, the dialog closes automatically.

In the following example, the prompt dialog is displayed and returns a value based on the OK or Cancel button click.

@using Syncfusion.Blazor.Popups
@using Syncfusion.Blazor.Buttons
@inject SfDialogService DialogService
    <div>
        <SfButton @onclick="@PromptBtn">Prompt</SfButton>
    </div>
    <div class="status" style ="padding-top:10px">
    @DialogStatus
</div>
@code {
    private string DialogStatus { get; set; }
    
    private async Task PromptBtn()
    {
        string promptText = await DialogService.PromptAsync("Enter your name:", "Join Chat Group");

        this.DialogStatus = $"Input from the user is returned as \"{promptText}\".";
    }
}
Prompt Dialog