You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor Server Side:MainLayout级联参数在Index中始终为Null

Blazor级联参数传递为空问题排查与解决

我创建了NewStuff.cs类,尝试从MainLayout把它传递给所有子组件(先测试Index.razor)。但调试确认MainLayout里的newStuff变量有数据且不为空,可Index组件里的这个级联参数始终是Null。

现有代码

MainLayout.razor

<div class="page">
    <main>
        <div style="height:200px;border-bottom-style:solid;background-color:steelblue">
        </div>
        <div>
            <NavMenu />
        </div>
        <CascadingValue Value="@newStuff">
        <article class="content px-4">
            @Body
        </article>
        </CascadingValue>
    </main>
</div>

@code{
    [CascadingParameter]
    private NewStuff newStuff { get; set; } = new();

    protected override async Task OnInitializedAsync()
    {
        ///填充newStuff的代码,确保其不为空。
    }
}

Index.razor

@code{
    [CascadingParameter]
    private Task<AuthenticationState> authenticationState { get; set; }
    [CascadingParameter]
    private NewStuff newStuff { get; set; }
    private String username { get; set; }
    private TenantInfoDTO tenantInfo { get; set; } = new();

    protected override async Task OnInitializedAsync()
    {
        var authState = await authenticationState;
        username = authState.User.Identity.Name;
        tenantInfo = await _tenantInfoRepository.Get(newStuff);
    }
}

更新补充:App.razor

@using App_Database.Data;

<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
            <NotAuthorized>
                <LoginRedirect/>
            </NotAuthorized>
        </AuthorizeRouteView>
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(MainLayout)">
            <p role="alert">Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>
</CascadingAuthenticationState>

@code {  
}

问题原因与解决方法

核心问题是MainLayout中的newStuff被错误标记为[CascadingParameter]——这个标记的作用是接收上级组件传递的级联值,而非作为数据源向下传递。Blazor的级联机制会优先尝试从上级获取值覆盖该属性,导致你自己初始化的数据源失效。

修改后的MainLayout.razor

<div class="page">
    <main>
        <div style="height:200px;border-bottom-style:solid;background-color:steelblue">
        </div>
        <div>
            <NavMenu />
        </div>
        <CascadingValue Value="@newStuff" IsFixed="false">
        <article class="content px-4">
            @Body
        </article>
        </CascadingValue>
    </main>
</div>

@code{
    // 移除[CascadingParameter]标记,改为普通私有属性
    private NewStuff newStuff { get; set; } = new();

    protected override async Task OnInitializedAsync()
    {
        ///填充newStuff的代码,确保其不为空。
    }
}

额外说明

  • 添加IsFixed="false"是为了让子组件能感知到newStuff的后续变化(默认IsFixed="true"仅传递初始值),如果你的newStuff是一次性初始化无需更新,也可以保留默认值。
  • 移除[CascadingParameter]后,newStuff就成为MainLayout自身的属性,通过<CascadingValue>包裹@Body即可正确向下传递给所有子组件。

内容的提问来源于stack exchange,提问作者gbm0102

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 09:52:58