Search code examples
referencedialogblazormudblazor

Close a Mudblazor Dialog


I am using Mudblazor in my Blazor app. I have the following code in a component inside ValidSubmit handler:

public async Task HandleValidSubmit()
{
    DialogService.Show<SavingDialog>("Saving Data");
    await Http.PostAsJsonAsync("api/Client/AddClient", CModel);

    //close the dialog here...
    //DialogService.Close(<need reference here>);
}

The DialogService opens the SavingDialog which is also a component. After the http call, I want to close the dialog. How do I do that? I can see the DialogService.Close(DialogReference dialog) in the documentation.

How do I get reference to the dialog box that I opened so I can close it?


Solution

  • Show returns a reference to the opened dialog!

    So all you need to do is this:

    public async Task HandleValidSubmit()
    {
       var dialogRef = DialogService.Show<SavingDialog>("Saving Data");
       await Http.PostAsJsonAsync("api/Client/AddClient", CModel);
    
       //close the dialog here...
       dialogRef.Close();
    }