Consolidate Edit and Add server pages into single routable component.
This commit is contained in:
parent
d6df2a448d
commit
86e41b22ff
@ -44,7 +44,7 @@
|
||||
</button>
|
||||
}
|
||||
|
||||
<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
|
||||
|
||||
@ -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
|
||||
@ -11,18 +6,27 @@ The navigation manager only will respond correctly if all its behaviours are the
|
||||
<NavigationLock
|
||||
OnBeforeInternalNavigation="OnBeforeInternalNavigation"
|
||||
ConfirmExternalNavigation="true">
|
||||
</NavigationLock>
|
||||
</NavigationLock>
|
||||
|
||||
<h3>Edit server</h3>
|
||||
<br/>
|
||||
<br/>
|
||||
@if (Id.HasValue)
|
||||
{
|
||||
<h3>Edit server</h3>
|
||||
}
|
||||
else
|
||||
{
|
||||
<h3>Add server</h3>
|
||||
}
|
||||
|
||||
@if (server != null)
|
||||
{
|
||||
<EditForm Model="server" FormName="formServer" OnValidSubmit="SubmitServer">
|
||||
<DataAnnotationsValidator></DataAnnotationsValidator>
|
||||
<ValidationSummary></ValidationSummary>
|
||||
<InputNumber @bind-Value="server.Id" hidden></InputNumber>
|
||||
|
||||
@if (server.Id > 0)
|
||||
{
|
||||
<InputNumber @bind-Value="server.Id" hidden></InputNumber>
|
||||
}
|
||||
|
||||
<FieldComponent Label="Name">
|
||||
<Control>
|
||||
@ -48,29 +52,50 @@ The navigation manager only will respond correctly if all its behaviours are the
|
||||
</Control>
|
||||
</FieldComponent>
|
||||
<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>
|
||||
}
|
||||
|
||||
<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)
|
||||
{
|
||||
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.
|
||||
@ -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>
|
||||
|
||||
<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}");
|
||||
}
|
||||
}
|
||||
@ -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">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user