- SetParametersAsync Name: Id
- SetParametersAsync Value: e4b049b4-0f21-41d5-8fd2-b3439fdd5b58
- **OnInitializedAsync OnInit called with ID: e4b049b4-0f21-41d5-8fd2-b3439fdd5b58
- OnInitializedAsync OnInit called with ID: null
When I use NavigationManager.NavigateTo in my Blazor Server project to navigate to a component with a routeparameter like
@page "/deal/{Id}/edit", the Id parameter is wiped to null on the second call to OnInitializedAsync.
NavigateTo Method:
public void EditDeal()
{
NavigationManager.NavigateTo($"/deal/{dealId}/edit");
}
<a @onclick="EditDeal"
OnInitializedMethod:
protected override async Task OnInitializedAsync()
{
Console.WriteLine($"OnInitializedAsync OnInit called with ID: {Id}");
if (Id == null)
{
dealInformation = dealInformation.SetDefaults();
}
else
{ // Get from DB
dealInformation = await DealService.Get(Id);
}
}
I have tried changing the Server rendering from ServerPrerendered to Server with no luck, and have been stuck for hours.
Thanks!
CodePudding user response:
As your only showing us some of your code here's my test setup for your issue which works as expected.
GUIDRouteComponent.razor
@page "/deal/{Id:guid}/edit"
<h3>GUID Route Component</h3>
<div >
@this.Id
</div>
@code {
[Parameter] public Guid Id { get; set; }
}
And my Index page:
@page "/Index"
@page "/"
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
<div >
<button @onclick="this.GoToDeal">Go to Deal</button>
</div>
<div >
<a href="/deal/e4b049b4-0f21-41d5-8fd2-b3439fdd5b58/edit"> Go to Deal</a>
</div>
@code {
[Inject] private NavigationManager? NavManager { get; set; }
protected override void OnInitialized()
=> base.OnInitialized();
private void GoToDeal()
=> this.NavManager?.NavigateTo("/deal/e4b049b4-0f21-41d5-8fd2-b3439fdd5b58/edit");
}
You say "the Id parameter is wiped to null on the second call to OnInitializedAsync." If you are navigating within your app;lication there should be no second call. This only occurs on the initial page load.
