Darkmatter5
Darkmatter5

Reputation: 1249

New help saving data in Mudblazor dialog

Here's my code:

@inject IThemeService ThemeService
@inject ISnackbar Snackbar

<MudDialog Style="min-width: 500px;">
    <TitleContent>
        <MudText Typo="Typo.h6">
            @if (@theme.Id == 0)
            {
                <MudIcon Icon="@Icons.Material.Filled.AddBox" Class="mr-3 mb-n1" />
                <span>Add a Theme?</span>
            }
            else
            {
                <MudIcon Icon="@Icons.Material.Filled.AddBox" Class="mr-3 mb-n1">Edit Theme?</MudIcon>
                <span>Edit a Theme?</span>
            }
        </MudText>
    </TitleContent>
    <DialogContent>
        <MudTextField Value="@theme.Id.ToString()" Label="Theme ID" ReadOnly="true" />
        <MudTextField Value="@theme.Title" Label="Theme Name" />
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel">Cancel</MudButton>
        <MudButton Color="Color.Error" OnClick="SaveTheme">Save Theme</MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] MudDialogInstance MudDialog { get; set; }
    [Parameter] public Theme theme { get; set; } = new Theme();

    private void Cancel()
    {
        MudDialog.Cancel();
    }

    private async void SaveTheme()
    {
        if (theme.Id == 0)
        {
            await ThemeService.CreateTheme(theme);
            Snackbar.Add("Theme Created!", Severity.Success);
        }
        else 
        {
            await ThemeService.UpdateTheme(theme);
            Snackbar.Add("Theme Updated!", Severity.Success);
        }

        MudDialog.Close(DialogResult.Ok(theme.Id));
        ThemeService.OnChange += StateHasChanged;
    }
}

Why would is not save the fields I enter in the dialog? It creates a new record in the db, but the Title field is empty.

Any help is appreciated!!

Upvotes: 0

Views: 2753

Answers (1)

Caius Jard
Caius Jard

Reputation: 74700

Your binding is one way (from variable to page control). The simplest option for making it two way is to @bind

    <MudTextField @bind-Value="theme.Id" Label="Theme ID" ReadOnly="true" />
    <MudTextField @bind-Value="theme.Title" Label="Theme Name" />

Upvotes: 0

Related Questions