1
0

Use NavigationLock on EditServer page to prevent user from navigating away before confirmation.

This commit is contained in:
Kevin Matsubara 2025-03-29 18:44:39 +01:00
parent 2da0063331
commit ae3f096722

View File

@ -1,9 +1,17 @@
@page "/servers/{id:int}"
@* Route constraints: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-9.0#route-constraints *@
@attribute [ExcludeFromInteractiveRouting]
@* 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/>
@ -68,4 +76,13 @@
// 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();
}
}
}