@page "/students/edit"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@inject AppDbContext Context
@inject NavigationManager NavigationManager
Edit Student - TSA Chapter Organizer
Edit
Student@(@Student == null ? "" : $" ({Student.Name})")
@if (Student is null)
{
Loading...
}
else
{
/* 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())
{
}
Back
Save
}
@code {
[SupplyParameterFromQuery]
private int Id { get; set; }
[SupplyParameterFromForm]
private Student? Student { get; set; }
protected override async Task OnInitializedAsync()
{
Student ??= await Context.Students.FirstOrDefaultAsync(m => m.Id == Id);
if (Student is null)
{
NavigationManager.NavigateTo("notfound");
}
}
// 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()
{
if (Student.OfficerRole == 0)
Student.OfficerRole = null;
Context.Attach(Student!).State = EntityState.Modified;
try
{
await Context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!StudentExists(Student!.Id))
{
NavigationManager.NavigateTo("notfound");
}
else
{
throw;
}
}
NavigationManager.NavigateTo("/students");
}
private bool StudentExists(int id)
{
return Context.Students.Any(e => e.Id == id);
}
}