Dear support team,
I am starting in Blazor Server and I have a complex view with lots of dialogs to input data, these dialogs are loaded within the page and obviously affects performance, so I wonder if it is possible to manage dialogs' content as razor does with partial views.
is it possible?
Thanks in advance.
using Microsoft.AspNetCore.Components; namespace Dialog.Components { public class DialogOptions { public string Header { get; set; } public RenderFragment Content { get; set; } public RenderFragment FooterTemplate { get; set; } public bool IsModal { get; set; } public bool ShowCloseIcon { get; set; } } } |
using System; using Microsoft.AspNetCore.Components; namespace Dialog.Components { public class DialogService { public event Action<DialogOptions> DialogInstance; public void Open(DialogOptions options) { // Invoke DialogComponent to update and show the dialog with options this.DialogInstance.Invoke(options); } } } |
@using Syncfusion.Blazor.Popups; @if (RenderDialog) { <SfDialog IsModal="@Options.IsModal" ShowCloseIcon="@Options.ShowCloseIcon" @bind-Visible="@Visibility"> <DialogTemplates> <Header> @Options.Header </Header> <Content> @Options.Content </Content> <FooterTemplate>@Options.FooterTemplate</FooterTemplate> </DialogTemplates> </SfDialog> } @code { [Inject] public DialogService DialogService { get; set; } private bool RenderDialog { get; set; } = false; private bool Visibility { get; set; } = false; // Parameter private DialogOptions Options = new DialogOptions(); protected override void OnInitialized() { // Update the parameter in local variable and render the dialog DialogService.DialogInstance += (DialogOptions options) => { InvokeAsync(() => { this.Options.Header = options.Header; this.Options.Content = options.Content; this.Options.IsModal = options.IsModal; this.Options.ShowCloseIcon = options.ShowCloseIcon; this.Options.FooterTemplate = options.FooterTemplate; this.RenderDialog = true; this.Visibility = true; this.StateHasChanged(); }); }; } } |
using Dialog.Components; namespace Dialog { public class Program { public static async Task Main(string[] args) { ... builder.Services.AddScoped<DialogService>(); ... } } } |
@using Dialog.Components … … <DialogComponent /> |
@page "/" @using Dialog.Components @inject DialogService DialogServices <button class="e-btn" @onclick="@SignInDialog"> Sign In Dialog </button> <button class="e-btn" @onclick="@SignUpDialog"> Sign Up Dialog </button> @code { private void SignInDialog() { this.DialogServices.Open(new DialogOptions() { Header = "Sign In", Content = @<SignIn />, // Fragment loaded from external component IsModal = true, ShowCloseIcon = true, FooterTemplate = @<button> OK </button> // Fragments }); } private void SignUpDialog() { this.DialogServices.Open(new DialogOptions() { Header = "Sign Up", Content = @<SignUp />, // Fragment loaded from external component IsModal = true, ShowCloseIcon = true, FooterTemplate = @<button> OK </button> // Fragments }); } } |
What a complete guide!
Thank you so much, Vinitha :)