Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

CascadingParameter is null problem in Blazor Server

$
0
0

I'm using Blazor Server on .NET 9, I have this components tree:

- <ShoppingCartState>   - <ShoppingCartPage>     - <ShoppingCartLayout>        - <ShoppingCart>   route start here  @page/shopping-cart          - <CartItems>

ShoppingCartState provides a CascadingValue called CartModelwhen I navigate to /shopping-cart page, the CartModel is always accessible in ShoppingCart component or after it, but not before it for example in ShoppingCartLayout it always null.

I'm confused and I don't know. I feel that because the page starts rendering from here, that parameter can be received from here. If this is the problem, what is the solution to receive it in the previous components?

ShoppingCartState code :

@inject IShoppingCartService CartService@implements IDisposable@rendermode InteractiveServer<div class=""><Loader IsLoading="IsLoading||CartModel is null">        @if (CartModel is not null)        {<CascadingValue Value="OnCartUpdated"><CascadingValue Value="CartModel">                    @ChildContent</CascadingValue></CascadingValue>        }</Loader></div>@code {    private bool IsLoading;    [Parameter]    public RenderFragment? ChildContent { get; set; }    public Action OnCartUpdated { get; set; } = null!;    public ShoppingCartModel? CartModel { get; set; } = new();    protected override void OnInitialized()    {        OnCartUpdated += StateHasChanged;    }    public void Dispose()    {        OnCartUpdated -= StateHasChanged;    }    protected override async Task OnAfterRenderAsync(bool firstRender)    {        if (firstRender)        {            await LoadCartAsync();        }    }    private async Task LoadCartAsync()    {        IsLoading = true;        CartModel = await CartService.GetCartAsync();        IsLoading = false;        StateHasChanged();    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>