@page "/students/edit" @attribute [Authorize] @using Microsoft.EntityFrameworkCore @using WebApp.Components.Shared.Components @using MudBlazor @using WebApp.Services @inject AppDbContext Context @inject NavigationManager NavigationManager @inject ISnackbar Snackbar @inject FormValidationService ValidationService @if (Student is null || _editContext is null) {

Loading...

return; } @* https://www.mudblazor.com/components/form *@ @* https://medium.com/@husainalbar/applying-mudblazor-for-crud-operations-in-our-blazor-project-a343037a52ef *@ -not an officer- @foreach (var officerRole in Enum.GetValues(typeof(OfficerRole)).Cast()) { } Save Cancel @code { [SupplyParameterFromQuery] private int Id { get; set; } [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } [SupplyParameterFromForm] private Student? Student { get; set; } private FormChangeTracker? _formChangeTracker; private EditContext? _editContext; private List _validationErrors = new(); protected override async Task OnInitializedAsync() { Student ??= await Context.Students.FirstOrDefaultAsync(m => m.Id == Id); if (Student is null) { NavigationManager.NavigateTo("notfound"); } else { _editContext = new EditContext(Student); } } private void OnInvalidSubmit(EditContext editContext) { _validationErrors = ValidationService.HandleInvalidSubmit(editContext, Student!, errors => _validationErrors = errors); StateHasChanged(); } // To protect from overposting attacks, enable the specific properties you want to bind to. // For more information, see https://learn.microsoft.com/aspnet/core/blazor/forms/#mitigate-overposting-attacks. private async Task UpdateStudent() { // Validate before processing if (_editContext != null) { var errors = ValidationService.CollectValidationErrors(_editContext, Student!); if (errors.Any()) { _validationErrors = ValidationService.HandleInvalidSubmit(_editContext, Student!, err => _validationErrors = err); StateHasChanged(); return; } } _validationErrors.Clear(); if (Student?.OfficerRole == 0) Student.OfficerRole = null; Context.Attach(Student!).State = EntityState.Modified; try { await Context.SaveChangesAsync(); Snackbar.Add($"Student '{Student!.FirstNameLastName}' saved successfully.", Severity.Success); _formChangeTracker?.AllowNavigation(); NavigationManager.NavigateTo(ReturnUrl ?? "/students"); } catch (DbUpdateConcurrencyException ex) { if (!ValidationService.HandleDbUpdateConcurrencyException( ex, () => StudentExists(Student!.Id), "student", () => NavigationManager.NavigateTo("notfound"))) { throw; } } catch (DbUpdateException ex) { ValidationService.HandleDbUpdateException( ex, "An error occurred while saving the student.", "student", "saving"); } catch (Exception ex) { ValidationService.HandleException(ex); } } private async Task HandleCancel() { // Discard all in-memory changes by reloading from database if (Student != null) { await Context.Entry(Student).ReloadAsync(); } _formChangeTracker?.AllowNavigation(); NavigationManager.NavigateTo(ReturnUrl ?? "/students"); } private bool StudentExists(int id) { return Context.Students.Any(e => e.Id == id); } }