为什么 Blazor WebAssembly @foreach{} 无法呈现列表?
Why a Blazor WebAssembly @foreach{} can't render a list?
我不明白为什么这个列表渲染不再工作并抛出一个奇怪的异常:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Nullable object must have a value.
System.InvalidOperationException: Nullable object must have a value.
at System.Nullable`1[T].get_Value () <0x28f3be8 + 0x0000a> in :0
at .Client.Components.Threads.BuildRenderTree (Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) [0x010c7] in C:\Users\vOId\Desktop\projects<redacted><redacted>\Client\Components\Threads.razor:106
at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0 (Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder) <0x2d39550 + 0x0001a> in :0
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch (Microsoft.AspNetCore.Components.Rendering.RenderBatchBuilder batchBuilder, Microsoft.AspNetCore.Components.RenderFragment renderFragment) <0x2d38ea8 + 0x00062> in :0
at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch (Microsoft.AspNetCore.Components.Rendering.RenderQueueEntry renderQueueEntry) <0x2d388a8 + 0x0004c> in :0
at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue () <0x2d35ce8 + 0x00098> in :0
列表的初始化是正常的,从服务器获取数据e将其添加到列表中进行渲染(ViewThreads
):
protected async override Task OnInitializedAsync()
{
var result = await ThreadService.GetThreads();
if (result.IsValid)
ViewThreads.AddRange((List<ViewThread>)result.Data);
//...
}
同时在@foreach
的第一条指令的渲染点,第106行异常,这是渲染
//...
@foreach (var thread in ViewThreads)
{
<p class="button is-light is-rounded neoFile petite-caps mb-3 @(CurrentThread != null && CurrentThread.Id == thread.Id ? "isSelected" : null)"
@onclick="() => SelectThread(thread)" @key="thread">
@thread.Title
</p>
}
//...
这部分代码没有改变,只是突然渲染该列表抛出异常并且不再渲染元素。
它可能是什么?
有人遇到过类似的问题吗?
初始化ViewThreads
对象:
public List<ViewThread> ViewThreads { get; set; } = new List<ViewThread>();
或将 foreach
环绕在 if (ViewThreads != null)
周围:
if (ViewThreads != null)
{
@foreach (var thread in ViewThreads)
{
<p class="button is-light is-rounded neoFile petite-caps mb-3 @(CurrentThread != null && CurrentThread.Id == thread.Id ? "isSelected" : null)"
@onclick="() => SelectThread(thread)" @key="thread">
@thread.Title
</p>
}
}
Nullable object must have a value.
您在此处发布的代码不会产生该错误。正如您自己所说,“这部分代码没有改变”
所以你用 ...
替换了某些部分。寻找可为 null 的 属性.
而“突然渲染那个列表抛出”很可能是由数据引起的。某些专栏在您意想不到的地方偷偷加入了空值。
我不明白为什么这个列表渲染不再工作并抛出一个奇怪的异常:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Nullable object must have a value. System.InvalidOperationException: Nullable object must have a value. at System.Nullable`1[T].get_Value () <0x28f3be8 + 0x0000a> in :0 at .Client.Components.Threads.BuildRenderTree (Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) [0x010c7] in C:\Users\vOId\Desktop\projects<redacted><redacted>\Client\Components\Threads.razor:106 at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0 (Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder) <0x2d39550 + 0x0001a> in :0 at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch (Microsoft.AspNetCore.Components.Rendering.RenderBatchBuilder batchBuilder, Microsoft.AspNetCore.Components.RenderFragment renderFragment) <0x2d38ea8 + 0x00062> in :0 at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch (Microsoft.AspNetCore.Components.Rendering.RenderQueueEntry renderQueueEntry) <0x2d388a8 + 0x0004c> in :0 at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue () <0x2d35ce8 + 0x00098> in :0
列表的初始化是正常的,从服务器获取数据e将其添加到列表中进行渲染(ViewThreads
):
protected async override Task OnInitializedAsync()
{
var result = await ThreadService.GetThreads();
if (result.IsValid)
ViewThreads.AddRange((List<ViewThread>)result.Data);
//...
}
同时在@foreach
的第一条指令的渲染点,第106行异常,这是渲染
//...
@foreach (var thread in ViewThreads)
{
<p class="button is-light is-rounded neoFile petite-caps mb-3 @(CurrentThread != null && CurrentThread.Id == thread.Id ? "isSelected" : null)"
@onclick="() => SelectThread(thread)" @key="thread">
@thread.Title
</p>
}
//...
这部分代码没有改变,只是突然渲染该列表抛出异常并且不再渲染元素。
它可能是什么? 有人遇到过类似的问题吗?
初始化ViewThreads
对象:
public List<ViewThread> ViewThreads { get; set; } = new List<ViewThread>();
或将 foreach
环绕在 if (ViewThreads != null)
周围:
if (ViewThreads != null)
{
@foreach (var thread in ViewThreads)
{
<p class="button is-light is-rounded neoFile petite-caps mb-3 @(CurrentThread != null && CurrentThread.Id == thread.Id ? "isSelected" : null)"
@onclick="() => SelectThread(thread)" @key="thread">
@thread.Title
</p>
}
}
Nullable object must have a value.
您在此处发布的代码不会产生该错误。正如您自己所说,“这部分代码没有改变”
所以你用 ...
替换了某些部分。寻找可为 null 的 属性.
而“突然渲染那个列表抛出”很可能是由数据引起的。某些专栏在您意想不到的地方偷偷加入了空值。