【发布时间】:2020-09-06 01:18:06
【问题描述】:
我正在尝试制作一个类似于https://docs.microsoft.com/en-us/aspnet/core/blazor/templated-components?view=aspnetcore-3.1 中的 blazor 模板化组件,但带有一个嵌套子表。
父表按预期工作,但在 ChildRowTemplate 的绑定上引发了 null ref 异常(在附加代码中突出显示)。该模型是一代理多复合体。
模板
@typeparam TItem
<table>
<thead>
<tr>@TableHeader</tr>
</thead>
<tbody>
@foreach (var item in Items)
{
<tr> The error
@if (OnCollapse.HasDelegate)
{
<td>
<button class="primary-button mb-1" @onclick="@(_=>HandleCollapse(item))">Toggle Complexes</button>
</td>
}
@RowTemplate(item)
</tr>
<tr>
@ChildRowTemplate(item)
</tr>
}
</tbody>
<tfoot>
<tr>@TableFooter</tr>
</tfoot>
</table>
@code {
[Parameter]
public RenderFragment TableHeader { get; set; }
[Parameter]
public RenderFragment<TItem> RowTemplate { get; set; }
[Parameter]
public RenderFragment<TItem> ChildRowTemplate { get; set; }
[Parameter]
public RenderFragment TableFooter { get; set; }
[Parameter]
public IReadOnlyList<TItem> Items { get; set; }
[Parameter]
public EventCallback<object> OnCollapse { get; set; }
void HandleCollapse(TItem item)
{
OnCollapse.InvokeAsync(item);
}
}
代码
<TableTemplate Items="@Agents" OnCollapse="toggle" Context="agent">
<TableHeader>
<th><button class="outline-button">Add Agent</button></th>
<th>Co. Name</th>
<th>Co. Number</th>
<th>Admin</th>
<th>Admin Email</th>
<th>Admin Phone</th>
</TableHeader>
<RowTemplate>
<td>@agent.CompanyName</td>
<td>@agent.CompanyNumber</td>
<td>@agent.AdminFirstName @agent.AdminLastName</td>
<td>@agent.AdminEmail</td>
<td>@agent.AdminPhoneNumber</td>
</RowTemplate>
<ChildRowTemplate Context="agent">
<TableTemplate Items="@agent.Complexes"> @*<--- NULL REF *@
<TableHeader>
<th>Complex Name</th>
<th>Is Active</th>
<th>Address</th>
</TableHeader>
<RowTemplate Context="complex">
<td>@complex.Name</td>
<td>@complex.IsActive</td>
<td>@complex.DisplayAddress</td>
</RowTemplate>
</TableTemplate>
</ChildRowTemplate>
<TableFooter></TableFooter>
</TableTemplate>
【问题讨论】:
-
您对所有代理的
agent.Complexes != null有多大把握? -
完全确定。
标签: asp.net-core blazor