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> </button>
} }
&nbsp; &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>
<td> <td>
<EditForm <EditForm

View File

@ -1,9 +1,4 @@
@page "/servers/{id:int}" @page "/server/{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] *@
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject IJSRuntime JSRuntime @inject IJSRuntime JSRuntime
@ -11,18 +6,27 @@ The navigation manager only will respond correctly if all its behaviours are the
<NavigationLock <NavigationLock
OnBeforeInternalNavigation="OnBeforeInternalNavigation" OnBeforeInternalNavigation="OnBeforeInternalNavigation"
ConfirmExternalNavigation="true"> ConfirmExternalNavigation="true">
</NavigationLock> </NavigationLock>
<h3>Edit server</h3> @if (Id.HasValue)
<br/> {
<br/> <h3>Edit server</h3>
}
else
{
<h3>Add server</h3>
}
@if (server != null) @if (server != null)
{ {
<EditForm Model="server" FormName="formServer" OnValidSubmit="SubmitServer"> <EditForm Model="server" FormName="formServer" OnValidSubmit="SubmitServer">
<DataAnnotationsValidator></DataAnnotationsValidator> <DataAnnotationsValidator></DataAnnotationsValidator>
<ValidationSummary></ValidationSummary> <ValidationSummary></ValidationSummary>
<InputNumber @bind-Value="server.Id" hidden></InputNumber>
@if (server.Id > 0)
{
<InputNumber @bind-Value="server.Id" hidden></InputNumber>
}
<FieldComponent Label="Name"> <FieldComponent Label="Name">
<Control> <Control>
@ -48,29 +52,50 @@ The navigation manager only will respond correctly if all its behaviours are the
</Control> </Control>
</FieldComponent> </FieldComponent>
<br/> <br/>
<button class="btn btn-primary" type="submit">Update</button> @if (server.Id > 0)
{
<button class="btn btn-primary" type="submit">Update</button>
}
else
{
<button class="btn btn-primary" type="submit">Save</button>
}
&nbsp; &nbsp;
<a href="/servers" class="btn btn-primary">Close</a> <a href="/servers" class="btn btn-primary">Close</a>
</EditForm> </EditForm>
} }
@code { @code {
[Parameter] // This is a root parameter. [Parameter]
public int Id { get; set; } public int? Id { get; set; }
[SupplyParameterFromForm(FormName = "formServer")] [SupplyParameterFromForm]
private Server? server { get; set; } private Server? server { get; set; }
protected override void OnParametersSet() 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() private void SubmitServer()
{ {
if (server != null) if (server != null)
{ {
ServersRepository.UpdateServer(server.Id, server); 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. // An exception is raised when debugging from VS Code, but not when using dotnet watch.

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