1
0

88 lines
2.6 KiB
Plaintext

@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] *@
@inject NavigationManager NavigationManager
@inject IJSRuntime JSRuntime
<NavigationLock
OnBeforeInternalNavigation="OnBeforeInternalNavigation"
ConfirmExternalNavigation="true">
</NavigationLock>
<h3>Edit server</h3>
<br/>
<br/>
@if (server != null)
{
<EditForm Model="server" FormName="formServer" OnValidSubmit="SubmitServer">
<DataAnnotationsValidator></DataAnnotationsValidator>
<ValidationSummary></ValidationSummary>
<InputNumber @bind-Value="server.Id" hidden></InputNumber>
<FieldComponent Label="Name">
<Control>
<InputText @bind-Value="server.Name" class="form-control"></InputText>
</Control>
<ValidationControl>
<ValidationMessage For="() => server.Name"></ValidationMessage>
</ValidationControl>
</FieldComponent>
<FieldComponent Label="City">
<Control>
<InputText @bind-Value="server.City" class="form-control"></InputText>
</Control>
<ValidationControl>
<ValidationMessage For="() => server.City"></ValidationMessage>
</ValidationControl>
</FieldComponent>
<FieldComponent Label="Online">
<Control>
<InputCheckbox @bind-Value="server.IsOnline" class="form-check-input"></InputCheckbox>
</Control>
</FieldComponent>
<br/>
<button class="btn btn-primary" type="submit">Update</button>
&nbsp;
<a href="/servers" class="btn btn-primary">Close</a>
</EditForm>
}
@code {
[Parameter] // This is a root parameter.
public int Id { get; set; }
[SupplyParameterFromForm(FormName = "formServer")]
private Server? server { get; set; }
protected override void OnParametersSet()
{
server ??= ServersRepository.GetServerById(this.Id);
}
private void SubmitServer()
{
if (server != null)
{
ServersRepository.UpdateServer(server.Id, server);
}
// An exception is raised when debugging from VS Code, but not when using dotnet watch.
NavigationManager.NavigateTo("/servers");
}
private async Task OnBeforeInternalNavigation(LocationChangingContext context)
{
var isConfirmed = await JSRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to leave this page?");
if (!isConfirmed)
{
context.PreventNavigation();
}
}
}