I have an application where almost every page is going to need to reference their organization record, so in MainLayout.razor I have this code
@code {
Organization CurrentOrganization { get; set; }
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
CurrentOrganization = await GetOrganization();
}
}
<CascadingValue Name="CurrentOrganization" Value="@CurrentOrganization">
// This wraps the entire page body
</CascadingValue>
Then in my page I reference the CascadingValue like this
@page "/organization"
@code {
[CascadingParameter(Name = "CurrentOrganization")]
Organization CurrentOrganization { get; set; }
protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
// Use CurrentOrganization
}
}
If I launch the website the OnInitializedAsync method in MainLayout is called (twice, and both times CurrentOrganization is set properly) and if I click on the Organization page, the OnParametersSetAsync method fires and the CurrentOrganization isn't null and everything works fine.
If I go directly to the Organization page URL in the browser though, OnInitializedAsync in MainLayout fires once, then the OnParametersSetAsync method fires but the CurrentOrganization is now null.
What's different about accessing the page directly through the URL rather than clicking a link to get to it? If MainLayout is setting the CascadingValue before the page is loaded, why is the CascadingParameter null?
CurrentOrganization = await GetOrganization();beforeawait base.OnInitializedAsync();? - agua from mars