Getting Started with Predefined Dialogs in Blazor

10 Feb 202313 minutes to read

This section briefly explains how to include the predefined dialogs in your Blazor Server App and Blazor WebAssembly App using Visual Studio.

Prerequisites

Create a new Blazor App in Visual Studio

Create the Blazor Server App or Blazor WebAssembly App using Visual Studio in one of the following ways:

Install Syncfusion Blazor Popups NuGet in the App

The Syncfusion Blazor components are available in nuget.org. To use Syncfusion Blazor components in the application, add a reference to the corresponding NuGet. Refer to NuGet packages topic for available NuGet packages list with component details and Benefits of using individual NuGet packages.

To add the predefined dialog component in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for Syncfusion.Blazor.Popups, then install it.

Register Syncfusion Blazor Service and Sycnfusion Dialog Service

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

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

Now, register the Syncfusion Blazor Service and Sycnfusion Dialog Service in the Blazor Server App or Blazor WebAssembly App. Here, the Syncfusion Blazor Service is registered by setting the IgnoreScriptIsolation property as true to load the scripts externally in the next steps.

NOTE

From 2022 Vol-1 (20.1) version, the default value of IgnoreScriptIsolation is changed to true. It is not necessary to set the IgnoreScriptIsolation property to refer scripts externally, since the default value has already been changed to true, and this property is obsolete.

Blazor Server App

  • For the .NET 6 app, open the ~/Program.cs file and import the Syncfusion Blazor Service and Sycnfusion Blazor Dialog Service.

  • For the .NET 5 and .NET 3.X app, open the ~/Startup.cs file and import the Syncfusion Blazor Service and Sycnfusion Blazor Dialog Service.

The DialogService must be registered as scoped service using the SfDialogService method for getting the predefined dialogs instance at any page.

using Syncfusion.Blazor
using Syncfusion.Blazor.Popups

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<SfDialogService>();
builder.Services.AddSyncfusionBlazor();

var app = builder.Build();
....
using Syncfusion.Blazor
using Syncfusion.Blazor.Popups

namespace BlazorApplication
{
    public class Startup
    {
        ...
        public void ConfigureServices(IServiceCollection services)
        {
         	services.AddRazorPages();
    		services.AddServerSideBlazor();
    		services.AddScoped<SfDialogService>();
            services.AddSyncfusionBlazor();
    		services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });
    		services.AddServerSideBlazor().AddHubOptions(o =>
        }
        ...
    }
}

Blazor WebAssembly App

Open the ~/Program.cs file and register the Syncfusion Blazor Service and Sycnfusion Blazor Dialog Service in the client web 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();
....
using Syncfusion.Blazor
using Syncfusion.Blazor.Popups

namespace WebApplication1
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            ....
            builder.Services.AddScoped<SfDialogService>();
            builder.Services.AddSyncfusionBlazor();
            await builder.Build().RunAsync();
        }
    }
}

Add Style Sheet

Check out the Blazor Themes topic to learn different ways (Static Web Assets, CDN, and CRG) to refer to themes in the Blazor application, and to have the expected appearance for the Syncfusion Blazor components. Here, the theme is referred to using the Static Web Assets. Refer to the Enable static web assets usage topic to use static assets in your project.

To add a theme to the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for Syncfusion.Blazor.Themes, then install it. The theme style sheet from NuGet can be referred to as follows.

NOTE

If you are using the Syncfusion.Blazor single NuGet, you don’t have to refer to the Syncfusion.Blazor.Themes](https://www.nuget.org/packages/Syncfusion.Blazor.Themes/) NuGet. Since the style sheets are already inside the assets of the Syncfusion.Blazor NuGet.

Blazor Server App

  • For the .NET 6 app, add the Syncfusion bootstrap5 theme in the <head> of the ~/Pages/_Layout.cshtml file.

  • For the .NET 3.X, .NET 5 and .NET 7 app, add the Syncfusion bootstrap5 theme in the <head> of the ~/Pages/_Host.cshtml file.

<head>
    ...
    <link href="_content/Syncfusion.Blazor.Themes/bootstrap5.css" rel="stylesheet" />
    <!--Refer theme style sheet as below if you are using Syncfusion.Blazor Single NuGet-->
    <!--<link href="_content/Syncfusion.Blazor/styles/bootstrap5.css" rel="stylesheet" />-->
</head>
<head>
    ...
    <link href="_content/Syncfusion.Blazor.Themes/bootstrap5.css" rel="stylesheet" />
    <!--Refer theme style sheet as below if you are using Syncfusion.Blazor Single NuGet-->
    <!--<link href="_content/Syncfusion.Blazor/styles/bootstrap5.css" rel="stylesheet" />-->
</head>

Blazor WebAssembly App

For the Blazor WebAssembly App, Refer to the theme style sheet from NuGet in the <head> of the wwwroot/index.html file in the client web app.

<head>
    ...
    <link href="_content/Syncfusion.Blazor.Themes/bootstrap5.css" rel="stylesheet" />
    <!--Refer theme style sheet as below if you are using Syncfusion.Blazor Single NuGet-->
    <!--<link href="_content/Syncfusion.Blazor/styles/bootstrap5.css" rel="stylesheet" />-->
</head>

Add Script Reference

Check out the Adding Script Reference topic to learn different ways to add script references in the Blazor Application. In this getting started walk-through, the required scripts are referred to using the Static Web Assets externally inside the <head> as follows. Refer to the Enable static web assets usage topic to use static assets in your project.

Blazor Server App

  • For the .NET 6, refer to the script in the <head> of the ~/Pages/_Layout.cshtml file.

  • For the .NET 3.X, .NET 5 and .NET 7 app, refer to the script in the <head> of the ~/Pages/_Host.cshtml file.

<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>
    <!--Use below script reference if you are using Syncfusion.Blazor Single NuGet-->
    <!--<script  src="_content/Syncfusion.Blazor/scripts/syncfusion-blazor.min.js"  type="text/javascript"></script>-->
</head>
<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>
    <!--Use below script reference if you are using Syncfusion.Blazor Single NuGet-->
    <!--<script  src="_content/Syncfusion.Blazor/scripts/syncfusion-blazor.min.js"  type="text/javascript"></script>-->
</head>

Blazor WebAssembly App

For the Blazor WebAssembly App, refer to the script in the <head> of the ~/index.html file.

<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>
    <!--Use below script reference if you are using Syncfusion.Blazor Single NuGet-->
    <!--<script  src="_content/Syncfusion.Blazor/scripts/syncfusion-blazor.min.js"  type="text/javascript"></script>-->
</head>

NOTE

Syncfusion recommends to reference scripts using the Static Web Assets, CDN, and CRG by disabling JavaScript isolation for better loading performance of the Blazor application.

Add Dialog Provider

SfDialogProvider allows to open predefined dialogs based on SfDialogService settings from any where in application. You can add SfDialogProvider in MainLayout.razor or any page. But it should be added only once in the app. If you add in MainLayout.razor, you can open predefined dialogs from any where in application. If you add in particular page, you can open dialogs only within the page.

  • Open the ~/_Imports.razor file or any other page under the ~/Pages folder where the component is to be added and import the Syncfusion.Blazor.Popups namespace.
@using Syncfusion.Blazor
@using Syncfusion.Blazor.Popups
  • Now, add SfDialogProvider in the ~/_MainLayout.razor file.
<Syncfusion.Blazor.Popups.SfDialogProvider/>

Open Predefined Dialog

Once you added SfDialogService and SfDialogProvider, you can open predefined dialogs from any where in application using AlertAsync,ConfirmAsync or PromptAsync methods in DialogServices.

Show alert dialog

An alert dialog box used to display an errors, warnings, and information alerts that needs user awareness. This can be achieved by using the DialogService.AlertAsync method. The alert dialog is displayed along with the OK button. When user clicks on OK button, alert dialog will get closed.

In the below code example, alert dialog displayed on button click action.

@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 box used to displays a specified message along with the OK and Cancel buttons, which returns a boolean value according to the user’s click action. When the user clicks the OK button, the true value is returned otherwise it will returns the false value. This can be achieved by using the DialogService.ConfirmAsync method. It is used to get approval from the user, and it appears before any critical action. After get approval from the user the dialog will disappear automatically.

In the below code example, the confirm dialog displayed on OK and Cancel button click action.

@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 the input from the user by using the DialogService.PromptAsync method. When the user clicks the OK button the input value from the dialog is returned. If the user clicks the Cancel button the null value is returned. After getting the input from the user the dialog will disappear automatically.

In the below code example, the prompt dialog displayed on OK and Cancel button click action.

@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