当表中充满数据时,页面会抛出NullReferenceException。所有数据都已填充(模拟数据(。不存在null值的情况。当它试图调用";context.consor.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",
LastName = "Fox";
Consultant = consultant
},
}.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, Culture=neutral,
> 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()
> }
找到的解决方案:
这是蚂蚁设计的问题。这是丢失的:
<Column TData="string?" DataIndex ="Consultant.FullName" />
您可以指定要绑定的属性,而不是使用@bind Field通过设置数据类型TData和数据索引串DataIndex,其可以绑定后代属性。列数据的类型为与TData的定义相同。
当绑定属性为ValueType且可为null时,TData应为设置为Nullable类型。例如:或"DataIndex="prop1"/>。
当Column使用DataIndex时,表的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