Home > Back-end >  Mudblazor dialog - Refresh on close
Mudblazor dialog - Refresh on close

Time:09-06

I am using Mudblazor in my Blazor app. The following code opens a dialog :

private async Task OpenDialog()
{
    var options = new DialogOptions { CloseOnEscapeKey = true };
    var dialog = DialogService.Show<AddNewAppDialog>("Adding new application", options);
    
    // TODO refresh data on dialog close
}

With very simple code for AddNewAppDialog component :

<MudDialog>
<DialogContent>
    <MudTextField @bind-Value="ApplicationName" Variant="Variant.Outlined" Label="Application Name" />

</DialogContent>
<DialogActions>
    <MudButton OnClick="Cancel">Cancel</MudButton>
    <MudButton Color="Color.Primary" OnClick="Submit">Ok</MudButton>
</DialogActions>
@code {

    public string ApplicationName { get; set; }

    [CascadingParameter] 
    MudDialogInstance MudDialog { get; set; }

    async Task Submit()
    {
        await CreateApplication();
        MudDialog.Close(DialogResult.Ok(true));
    }

    void Cancel() => MudDialog.Cancel();

 }

How do I get an event in parent component when my dialog is closed ?

CodePudding user response:

You have to use await dialog.Result;:

private async Task OpenDialog()
{
    var options = new DialogOptions { CloseOnEscapeKey = true };
    var dialog = DialogService.Show<AddNewAppDialog>("Adding new application", options);
    
    // wait modal to close
    var result = await dialog.Result;

    // you can check if the modal was cancelled
    var isCancelled = result.Cancelled;
    
    // refresh your data
}

https://mudblazor.com/components/dialog#passing-data

  • Related