Mudblazor dialog - Refresh on close
Asked Answered
A

1

7

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 ?

Andrews answered 5/9, 2022 at 8:27 Comment(0)
F
9

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

Fawne answered 5/9, 2022 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.