Created
February 20, 2024 20:37
-
-
Save isaacrlevin/3b86eeecf9ec450d27c1860d595f0b1d to your computer and use it in GitHub Desktop.
Blazor Boolean Form Submission
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page "/Account/Manage" | |
@using System.ComponentModel.DataAnnotations | |
@using Microsoft.AspNetCore.Identity | |
@using Scheduler.Data | |
@inject UserManager<ApplicationUser> UserManager | |
@inject SignInManager<ApplicationUser> SignInManager | |
@inject IdentityUserAccessor UserAccessor | |
@inject IdentityRedirectManager RedirectManager | |
<PageTitle>Profile</PageTitle> | |
<h3>Profile</h3> | |
<StatusMessage /> | |
<div class="row"> | |
<div class="col-md-6"> | |
<EditForm Model="Input" FormName="profile" OnValidSubmit="OnValidSubmitAsync" method="post"> | |
<DataAnnotationsValidator /> | |
<ValidationSummary class="text-danger" role="alert" /> | |
<div class="form-floating mb-3"> | |
<input type="text" value="@username" class="form-control" placeholder="Please choose your username." disabled /> | |
<label for="username" class="form-label">Username</label> | |
</div> | |
<div class="form-floating mb-3"> | |
<InputText @bind-Value="Input.DisplayName" class="form-control" placeholder="Please enter your preferred name." /> | |
<label for="displayName" class="form-label">Name</label> | |
<ValidationMessage For="() => Input.DisplayName" class="text-danger" /> | |
</div> | |
<div class="form-floating mb-3"> | |
<InputText @bind-Value="Input.PhoneNumber" class="form-control" placeholder="Please enter your phone number." /> | |
<label for="phone-number" class="form-label">Phone number</label> | |
<ValidationMessage For="() => Input.PhoneNumber" class="text-danger" /> | |
</div> | |
<div class="form-floating mb-3"> | |
Would you like to be emailed? | |
<InputCheckbox class="form-check-input" @bind-Value="Input.WantEmail" /> | |
</div> | |
<button type="submit" class="w-100 btn btn-lg btn-primary">Save</button> | |
</EditForm> | |
</div> | |
</div> | |
@code { | |
private ApplicationUser user = default!; | |
private string? username; | |
private string? phoneNumber; | |
private string? displayName; | |
private bool wantEmail; | |
[CascadingParameter] | |
private HttpContext HttpContext { get; set; } = default!; | |
[SupplyParameterFromForm] | |
private InputModel Input { get; set; } = new(); | |
protected override async Task OnInitializedAsync() | |
{ | |
user = await UserAccessor.GetRequiredUserAsync(HttpContext); | |
username = await UserManager.GetUserNameAsync(user); | |
phoneNumber = await UserManager.GetPhoneNumberAsync(user); | |
displayName = user.DisplayName; | |
wantEmail = user.WantEmail; | |
if (Input.PhoneNumber == null) | |
{ | |
Input.PhoneNumber = phoneNumber; | |
} | |
Input.DisplayName ??= displayName; | |
//Need to sync Input.WantEmail to Identity field in datastore, but don't want it to overwrite the value on form submission | |
Input.WantEmail = wantEmail; | |
} | |
private async Task OnValidSubmitAsync() | |
{ | |
if (Input.PhoneNumber != phoneNumber) | |
{ | |
var setPhoneResult = await UserManager.SetPhoneNumberAsync(user, Input.PhoneNumber); | |
if (!setPhoneResult.Succeeded) | |
{ | |
RedirectManager.RedirectToCurrentPageWithStatus("Error: Failed to set phone number.", HttpContext); | |
} | |
} | |
//only update the identity value if Input.WantEmail is different from the current value | |
if (Input.WantEmail != wantEmail) | |
{ | |
user.WantEmail = Input.WantEmail; | |
} | |
await UserManager.UpdateAsync(user); | |
await SignInManager.RefreshSignInAsync(user); | |
//RedirectManager.RedirectToCurrentPageWithStatus("Your profile has been updated", HttpContext); | |
} | |
private sealed class InputModel | |
{ | |
[NullablePhone] | |
[Display(Name = "Phone number")] | |
public string? PhoneNumber { get; set; } | |
[Display(Name = "Display Name")] | |
public string? DisplayName { get; set; } | |
[Display(Name = "Receive email notifications")] | |
public bool WantEmail { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment