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

Blazor QuickGrid无法正确加载数据问题求助

Blazor QuickGrid 绑定IEnumerable/IQueryable后无法显示数据的排查思路

我使用VS2022 17.5 Preview 1开发,将数据源设为IEnumerable后,Blazor QuickGrid仍无法显示数据。尝试改为IQueryable也未解决问题,接口返回800条记录,但网格显示空白,求排查思路。

参考官方示例实现的代码如下:

<QuickGrid Items="@playersList">
  <PropertyColumn  Property="@(c => c.FullName)" Sortable="true"  />       
</QuickGrid>

@code {

public IEnumerable<Players> playersList { get; set; }

protected override async Task OnInitializedAsync()
{
    Authentication auth = new Authentication();
    auth.Email = myuser.Username;
    myuser.Password = myuser.Password;
    string jwtTown =await  api.GenerateBarrerToken(myuser);
    auth.JWTToken = jwtTown;
    db.SaveAutheneticationAysnc(auth);

    playersList = await api.GetALLPlayers();
    playersList = playersList.AsEnumerable();
}

Players模型定义:

public class Players
{   
    public int Id { get; set; }
    public int? Type { get; set; }
    public int? OrginizationId { get; set; }

    public int? PersonId { get; set; }
    public int? CoachId { get; set; }
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public int? TeamId { get; set; }
    public string? PlayerLevel { get; set; }
    public string? FullName
    {
        get { return FirstName + " " + Surname; }
    }
}

项目配置文件:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.QuickGrid" Version="0.1.0-alpha.22351.1" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
      <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
      <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
      <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.0" />
      <PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="2.1.1" />
      <PackageReference Include="SQLitePCLRaw.provider.dynamic_cdecl" Version="2.1.2" />
      <PackageReference Include="Syncfusion.Blazor.Core" Version="20.3.0.56" />
      <PackageReference Include="Syncfusion.Blazor.Grid" Version="20.3.0.56" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\APP.Bal\APP.Bal.csproj" />
    <ProjectReference Include="..\APP.Dal\APP.Dal.csproj" />
    <ProjectReference Include="..\APP.Models\APP.Models.csproj" />
    <ProjectReference Include="..\APPServices\APP.Services.csproj" />
  </ItemGroup>

</Project>

排查思路

  • 触发组件状态更新:Blazor需要手动通知组件刷新UI。playersList是普通自动属性,赋值后不会触发重新渲染,可在赋值后调用StateHasChanged(),或者将playersList改为ObservableCollection<Players>。
  • 验证计算属性有效性:FullName依赖FirstName和Surname,如果这两个字段为null,FullName会是空字符串,导致网格看似空白。可临时添加绑定Id的列测试:<PropertyColumn Property="@(c => c.Id)" />,确认是否有数据渲染。
  • 检查QuickGrid版本兼容性:当前使用的是alpha版本(0.1.0-alpha.22351.1),可能存在已知bug。尝试升级到最新稳定版,或确认该版本与.NET 7的适配性。
  • 确认数据加载正确性:在赋值playersList后添加Console.WriteLine(playersList.Count()),验证接口返回的数据是否正确加载到内存中。
  • 处理Nullable空值:项目启用了<Nullable>enable</Nullable>,需确保FirstName和Surname的空值处理,修改FullName计算逻辑为return $"{FirstName ?? ""} {Surname ?? ""}".Trim();,避免出现无效空字符串。
  • 排查组件库冲突:项目同时引用了Syncfusion.Blazor.Grid,可能存在样式或组件逻辑冲突。可临时移除Syncfusion相关包,测试QuickGrid是否正常显示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 08:25:21