MessageBox

Message box is used for quick user confirmation actions.

Note

The MessageBox depends on IDialogService and MudDialogProvider

Any global configuration done on the MudDialogProvider will affect all MessageBoxes in your application.

Check the Dialog Configuration section for instructions on how to configure your dialogs globally.

Message Box

The simplest way to show a message box is to use IDialogService.ShowMessageBox. It is a purely procedural way of showing a message box and awaiting the user's decision. The number of buttons (Yes, No, Cancel) and their text are simply controlled via providing a string or leaving them null. The result is a nullable bool which corresponds directly to the buttons: Yes => true, No => false, Cancel => null.

Message box hasn't been opened yet
<MudButton Variant="Variant.Filled" Color="Color.Error" OnClick="OnButtonClicked" >Delete</MudButton>

<MudChip>@state</MudChip>
@code
{
    [Inject] private IDialogService DialogService { get; set; }

    string state = "Message box hasn't been opened yet";


    private async void OnButtonClicked()
    {
        bool? result = await DialogService.ShowMessageBox(
            "Warning", 
            "Deleting can not be undone!", 
            yesText:"Delete!", cancelText:"Cancel");
        state= result==null ? "Cancelled" : "Deleted!";
        StateHasChanged();
    }
}
Custom Message Box

MudMessageBox can also be inlined in Razor to be able to customize all its properties with render fragments. The following example shows how we customize the Yes button's color and icon. All visual elements of the message box can be customized. For Title there is TitleContent, for Message there is MessageContent, etc.

Message box hasn't been opened yet
<MudButton Variant="Variant.Filled" Color="Color.Error" OnClick="OnButtonClicked" >Delete</MudButton>
<MudChip>@state</MudChip>

<MudMessageBox @ref="mbox" Title="Warning" CancelText="Cancel">
    <MessageContent>
        Deleting can <b><i>not</i></b> be undone!
    </MessageContent>
    <YesButton>
        <MudButton Variant="Variant.Filled" Color="Color.Error" StartIcon="@Icons.Material.Filled.DeleteForever">Delete!</MudButton>
    </YesButton>
</MudMessageBox>
@code
{
    MudMessageBox mbox { get; set; }
    string state = "Message box hasn't been opened yet";

    private async void OnButtonClicked()
    {
        bool? result = await mbox.Show();
        state= result==null ? "Cancelled" : "Deleted!";
        StateHasChanged();
    }
}
Multiline Message

Instead of a string message you can also use MarkupString for the message box content. This is especially useful if you want to display multiline messages or do some basic styling, like highlighting a part of the message, without having to create a full-fledged custom dialog.
This example shows how a multiline MessageBox can still be a one-liner in code, thanks to MarkupString.

Message box hasn't been opened yet
<MudButton Variant="Variant.Filled" Color="Color.Info" OnClick="OnButtonClicked">I am Balrog</MudButton>

<MudChip>@state</MudChip>
@code
{
    [Inject] private IDialogService DialogService { get; set; }

    string state = "Message box hasn't been opened yet";

    private async void OnButtonClicked()
    {
        bool? result = await DialogService.ShowMessageBox(
            "Secure The Ring", 
            (MarkupString) $"You <br /> Shall <br /> not <br /> <b>Pass!<b/>",
            yesText:"Fire Whip!", cancelText:"Smash Ground");
        state= result==null ? "Returned to Moria" : "Fighting With Gandalf!";
        StateHasChanged();
    }
}
An unhandled error has occurred. Reload 🗙