微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Blazor Webassemby:对象引用未设置为对象 i

如何解决Blazor Webassemby:对象引用未设置为对象 i

当表格充满数据时,页面会抛出 NullReferenceException。填充所有数据(模拟数据)。没有空值的情况。当它试图调用“context.consultant.FullName”时,它抛出一个异常。 我也调试过了。所有数据都设置好了。

页面

  @if (list != null)
  {
        <AntDesign.Table @ref="table"
                         TItem="Customer"
                         DataSource="@list">

        <Column @bind-Field="@context.Id" />
        <Column @bind-Field="@context.Name"  />
        <Column @bind-Field="@context.LastName"  />
        
        <Column Field="@context.consultant.FullName"  />  --> throws here the exception.
    </AntDesign.Table>
}
@code{

 ...

  protected override async Task OnInitializedAsync()
  {
    list = GetCustomers()
  } 

   private List<Customer> GetCustomers()
   {
     var consultant = new Consultant
            {
                ID = "183",FirstName = "Service",LastName = "Team";
            };
            
            
        return new Customer[]
        {
            new(){
            ID = "3",FirstName = "Peter",LastName = "Fox";
            Consultant = consultant
            },new(){
            ID = "4",FirstName = "John",}.ToList();
    
    }

模型:

public Customer{
    public string ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Consultant Consultant { get; set; }
    public DateTime LastChangedDate { get; set; }
}

public record Consultant
{
    public string ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName {
        get => $"{FirstName} {LastName}";
    }
}

错误信息:

>   crit:
> Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
>       Unhandled exception rendering component: Object reference not set to an instance of an object. System.NullReferenceException: Object
> reference not set to an instance of an object.    at
> Test.Client.Pages.Customer.<>c__displayClass0_1.<buildrenderTree>b__21(RenderTreeBuilder
> __builder2)    at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent(Int32
> sequence,RenderFragment fragment)    at
> AntDesign.Table`1[[Test.Shared.Models.Customer,Roma.Portal.Shared,> Version=1.0.0.0,Culture=neutral,> PublicKeyToken=null]].<buildrenderTree>b__255_5(RenderTreeBuilder
> __builder6)    at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent(Int32
> sequence,RenderFragment fragment)    at
> Microsoft.AspNetCore.Components.CascadingValue`1[[System.Boolean,> System.Private.CoreLib,Version=5.0.0.0,> PublicKeyToken=7cec85d7bea7798e]].Render(RenderTreeBuilder builder)   
> at
> Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder
> batchBuilder,RenderFragment renderFragment)    at
> Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry
> renderQueueEntry)    at
> Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
> }

找到解决方案:

这是一个 AntDesign 问题。这是缺失的:

 <Column  TData="string?" DataIndex ="Consultant.FullName" />

可以指定要绑定的属性,而不是使用@bind-Field 通过设置数据类型 TData 和数据索引字符串 DataIndex, 它可以绑定后代属性。列数据的类型是 与TData的定义相同。

当绑定属性为 ValueType 且为 Nullable 时,TData 应为 设置为可空类型。例如:或“ DataIndex="prop1" />。

当Column使用DataIndex时,Table的OnChange事件参数 QuerModel.sortModel[].FieldName 等于 DataIndex。

有关访问模式的详细信息,请参阅基于路径的属性访问 DataIndex 支持

解决方法

我遇到了类似的问题,但服务器端 blazor。我最终想到的是创建一个新模型,其中包含您想要从两个类绑定到表的参数,在您的情况下,客户和顾问这样说;

    
   //from customer 
    public string ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    //from consultant
    public string ConsFirstName { get; set; }
    public string ConsLastName { get; set; }
    public string ConsFullName {
        get => $"{ConsFirstName} {ConsLastName}";
   
}```

then use it to map your result to the table by assigning the desired parameters from customer and consultant to the new MappingModel

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。