1
0

Consolidate Edit and Add server pages into single routable component.

This commit is contained in:
Kevin Matsubara 2025-03-29 19:26:52 +01:00
parent d6df2a448d
commit 86e41b22ff
5 changed files with 44 additions and 78 deletions

View File

@ -44,7 +44,7 @@
</button>
}
&nbsp;
<a href="@($"/servers/{Server.Id}")" class="btn btn-primary">Edit</a>
<a href="@($"/server/{Server.Id}")" class="btn btn-primary">Edit</a>
</td>
<td>
<EditForm

View File

@ -1,9 +1,4 @@
@page "/servers/{id:int}"
@* Route constraints: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-9.0#route-constraints *@
@* Note that this attribute now needs to be removed, because Routing is globally interactive.
The navigation manager only will respond correctly if all its behaviours are the same. *@
@* @attribute [ExcludeFromInteractiveRouting] *@
@page "/server/{id:int?}"
@inject NavigationManager NavigationManager
@inject IJSRuntime JSRuntime
@ -13,16 +8,25 @@ The navigation manager only will respond correctly if all its behaviours are the
ConfirmExternalNavigation="true">
</NavigationLock>
@if (Id.HasValue)
{
<h3>Edit server</h3>
<br/>
<br/>
}
else
{
<h3>Add server</h3>
}
@if (server != null)
{
<EditForm Model="server" FormName="formServer" OnValidSubmit="SubmitServer">
<DataAnnotationsValidator></DataAnnotationsValidator>
<ValidationSummary></ValidationSummary>
@if (server.Id > 0)
{
<InputNumber @bind-Value="server.Id" hidden></InputNumber>
}
<FieldComponent Label="Name">
<Control>
@ -48,30 +52,51 @@ The navigation manager only will respond correctly if all its behaviours are the
</Control>
</FieldComponent>
<br/>
@if (server.Id > 0)
{
<button class="btn btn-primary" type="submit">Update</button>
}
else
{
<button class="btn btn-primary" type="submit">Save</button>
}
&nbsp;
<a href="/servers" class="btn btn-primary">Close</a>
</EditForm>
}
@code {
[Parameter] // This is a root parameter.
public int Id { get; set; }
[Parameter]
public int? Id { get; set; }
[SupplyParameterFromForm(FormName = "formServer")]
[SupplyParameterFromForm]
private Server? server { get; set; }
protected override void OnParametersSet()
{
server ??= ServersRepository.GetServerById(this.Id);
if (this.Id.HasValue)
{
server ??= ServersRepository.GetServerById(this.Id.Value);
}
else
{
server ??= new Server() { IsOnline = false };
}
}
private void SubmitServer()
{
if (server != null)
{
if (this.Id.HasValue)
{
ServersRepository.UpdateServer(server.Id, server);
}
else
{
ServersRepository.AddServer(server);
}
}
// An exception is raised when debugging from VS Code, but not when using dotnet watch.
NavigationManager.NavigateTo($"/servers/back_from/{this.server?.City}");

View File

@ -1,59 +0,0 @@
@page "/servers/add"
@using System.ComponentModel.DataAnnotations
@attribute [ExcludeFromInteractiveRouting]
@inject NavigationManager NavigationManager
<h3>Add server</h3>
<br/>
<br/>
<EditForm Model="server" FormName="formServer" OnValidSubmit="SubmitServer">
<DataAnnotationsValidator></DataAnnotationsValidator>
<ValidationSummary></ValidationSummary>
<div class="row mb-3">
<div class="col-2">
<label class="col-form-label">Name</label>
</div>
<div class="col-6">
<InputText @bind-Value="server.Name" class="form-control"></InputText>
</div>
<div class="col">
<ValidationMessage For="() => server.Name"></ValidationMessage>
</div>
</div>
<div class="row mb-3">
<div class="col-2">
<label class="col-form-label">City</label>
</div>
<div class="col-6">
<InputText @bind-Value="server.City" class="form-control"></InputText>
</div>
<div class="col">
<ValidationMessage For="() => server.City"></ValidationMessage>
</div>
</div>
<br/>
<button class="btn btn-primary" type="submit">Add</button>
&nbsp;
<a href="/servers" class="btn btn-primary">Close</a>
</EditForm>
@code {
[SupplyParameterFromForm(FormName = "formServer")]
private Server server { get; set; } = new Server() { IsOnline = false };
private void SubmitServer()
{
if (server != null)
{
ServersRepository.AddServer(server);
}
// An exception is raised when debugging from VS Code, but not when using dotnet watch.
NavigationManager.NavigateTo($"/servers/back_from/{this.server?.City}");
}
}

View File

@ -18,7 +18,7 @@
style="width: 600px">
</SearchBarComponent>
<br/>
<a href="@($"/servers/add")" class="btn btn-primary">Add</a>
<a href="@($"/server")" class="btn btn-primary">Add</a>
<br/>
<CascadingValue Name="SelectedCity" Value="@selectedCity">