keshish
keshish
CC#
Created by keshish on 7/20/2023 in #help
❔ Custom SelectListDialog Enum
I am using MudBlazor and I am having problems with creating a Custom Dialog which has a parameter of Enum and renders the Enum Values as the SelectList or DropDown, my Enums are,
public enum InputSelectList
{
Range,
Values
}

public enum ParameterSelectList
{
Fix,
Range,
Values
}
public enum InputSelectList
{
Range,
Values
}

public enum ParameterSelectList
{
Fix,
Range,
Values
}
and an example of Custom Dialog in MudBlazor
@inject ApplicationDbContext DataContext;
@inject IStringLocalizer<Resources> Loc

<MudDialog>
<DialogContent>
<MudTextField @bind-Value="_role" Label="Role" />
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="Color.Primary" OnClick="Submit">Ok</MudButton>
</DialogActions>
</MudDialog>

@code {
[CascadingParameter] MudDialogInstance MudDialog { get; set; }
private string _role { get; set; }

[Inject] ISnackbar Snackbar { get; set; }

async Task Submit()
{
string normalizedRoleName = _role.ToUpper().Trim();

if (!await DataContext.Roles.AnyAsync(r => r.NormalizedName == normalizedRoleName))
{
var newRole = new IdentityRole(_role);
newRole.NormalizedName = normalizedRoleName;

var result = await DataContext.Roles.AddAsync(newRole);
await DataContext.SaveChangesAsync();

if (result != null)
{
Snackbar.Add(@Loc["RoleAddedSuccessfully"]);
MudDialog.Close(DialogResult.Ok(newRole));
}
else
{
Snackbar.Add(@Loc["ErrorAddingTheRole"]);
}
}
else
{
Snackbar.Add(@Loc["RoleAlreadyExists"]);
}
}

void Cancel() => MudDialog.Cancel();
}
@inject ApplicationDbContext DataContext;
@inject IStringLocalizer<Resources> Loc

<MudDialog>
<DialogContent>
<MudTextField @bind-Value="_role" Label="Role" />
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="Color.Primary" OnClick="Submit">Ok</MudButton>
</DialogActions>
</MudDialog>

@code {
[CascadingParameter] MudDialogInstance MudDialog { get; set; }
private string _role { get; set; }

[Inject] ISnackbar Snackbar { get; set; }

async Task Submit()
{
string normalizedRoleName = _role.ToUpper().Trim();

if (!await DataContext.Roles.AnyAsync(r => r.NormalizedName == normalizedRoleName))
{
var newRole = new IdentityRole(_role);
newRole.NormalizedName = normalizedRoleName;

var result = await DataContext.Roles.AddAsync(newRole);
await DataContext.SaveChangesAsync();

if (result != null)
{
Snackbar.Add(@Loc["RoleAddedSuccessfully"]);
MudDialog.Close(DialogResult.Ok(newRole));
}
else
{
Snackbar.Add(@Loc["ErrorAddingTheRole"]);
}
}
else
{
Snackbar.Add(@Loc["RoleAlreadyExists"]);
}
}

void Cancel() => MudDialog.Cancel();
}
3 replies