I am simply trying to close this dialog,specifically when i click on the cancel button. i have read the documentation and i dont think i understand how it works any help is greatly appreciated. I have tried looking at the Mud docs , stack posts youtube videos and i cant seem to figure out what the issue is. the close functionality is applied to the close cancel button but nothing is happening.
dialog component
@using MudBlazor
<MudDialog>
<TitleContent>
Dialog Title
</TitleContent>
<DialogContent>
Dialog Content
</DialogContent>
<DialogActions>
<MudButton style="background-color: red" OnClick="Cancel">Cancel</MudButton>
<MudButton style="background-color: green;" OnClick="Submit">Ok</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter]
private MudDialogInstance? MudDialog { get; set; }
private void Submit() => MudDialog?.Close(DialogResult.Ok(true));
private void Cancel() => MudDialog?.Cancel();
}
parent component
@using BlazorContactsApp.Services
@using BlazorContactsApp.Components
@using MudBlazor
@inject ContactService ContactService
@inject IDialogService DialogService
@page "/"
<div style="margin: 20px">
<MudButton OnClick="OpenDialogAsync" Variant="Variant.Filled" Color="Color.Primary">
Open Simple Dialog
</MudButton>
<h1>Hello, world!</h1>
<p>Welcome to your new app.</p>
</div>
@if (contacts == null)
{
<p><em>Loading...</em></p>
}
else
{
@foreach (var contact in contacts)
{
<MudCard Color="grey" style="border: 1px solid grey; margin: 2rem; width: 20vw; border-radius: 5px">
<MudCardContent>
<MudText Typo="Typo.body2">Name : @contact.Name</MudText>
</MudCardContent>
<MudDivider />
<MudCardContent>
<MudText Typo="Typo.body2">Number : @contact.Number</MudText>
</MudCardContent>
</MudCard>
}
}
@code {
private List<ContactsApi.Models.Contact> contacts = new List<ContactsApi.Models.Contact>();
protected override async Task OnInitializedAsync()
{
contacts = await ContactService.GetContactsAsync();
}
private async Task OpenDialogAsync()
{
var options = new DialogOptions { CloseOnEscapeKey = true, CloseButton = true };
var parameters = new DialogParameters();
var dialog = DialogService.Show<AddContactDialog>("Dialog Title", parameters, options);
var result = await dialog.Result;
}
}
Deployed a Mud Blazor 8 solution Server with Global Interactivity.
I’ve built the following MRE from your question code.
AddContactDialog.razor
<MudDialog>
<TitleContent>
Dialog Title
</TitleContent>
<DialogContent>
Dialog Content
</DialogContent>
<DialogActions>
<MudButton style="background-color: red" OnClick="Cancel">Cancel</MudButton>
<MudButton style="background-color: green;" OnClick="Submit">Ok</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter]
private MudDialogInstance? MudDialog { get; set; }
private void Submit() => MudDialog?.Close(DialogResult.Ok(true));
private void Cancel() => MudDialog?.Cancel();
}
Home:
@page "/"
@inject IDialogService DialogService
<div style="margin: 20px">
<MudButton OnClick="OpenDialogAsync" Variant="Variant.Filled" Color="Color.Primary">
Open Simple Dialog
</MudButton>
<h1>Hello, world!</h1>
<p>Welcome to your new app.</p>
</div>
@code {
private async Task OpenDialogAsync()
{
var options = new DialogOptions { CloseOnEscapeKey = true, CloseButton = true };
var parameters = new DialogParameters();
var dialog = DialogService.Show<AddContactDialog>("Dialog Title", parameters, options);
var result = await dialog.Result;
}
}
Opens and closes as expected. Try it.
The only conclusions I can come to is:
- Missing critical information or
- the code you’re showing isn’t what your running.