gpt4 book ai didi

c# - Blazor 如何使用绑定(bind)到 bool 数组

转载 作者:行者123 更新时间:2023-12-04 00:02:30 26 4
gpt4 key购买 nike

我有一个对象数组,其中有一个 IsChecked 属性。我用一个数组填充表格,对于 IsChecked 属性,我会通过 @bind 来输入。

当我尝试切换值时,会出现错误。如果我立即更改值 IsChecked = true,则勾选标记。

for(var i = 0; i < firms.Length; i++)
{
<tr>
<td>@firms[i].ShortTitle</td>
<th scope="row">
<div class="custom-control custom-checkbox">
<input type="checkbox" ... @bind="@firms[i].IsChecked" />
..
</div>
</th>
</tr>
}

错误:

Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer: Warning: Unhandled exception rendering component: Index was outside the bounds of the array.

System.IndexOutOfRangeException: Index was outside the bounds of the array. at ....<>c__DisplayClass0_1.b__6(Boolean __value) at Microsoft.AspNetCore.Components.EventCallbackFactoryBinderExtensions.<>c__DisplayClass22_0`1.b__0(ChangeEventArgs e) --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle) Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost: Error: Unhandled exception in circuit 'LzL8iNZr7FmkJFJRS3QTW3QzJwP9R-p3CaclRHcE1_A.'.

System.IndexOutOfRangeException: Index was outside the bounds of the array. at ...<>c__DisplayClass0_1.b__6(Boolean __value) at Microsoft.AspNetCore.Components.EventCallbackFactoryBinderExtensions.<>c__DisplayClass22_0`1.b__0(ChangeEventArgs e) --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

我做错了什么?

最佳答案

在您的代码中的某处(您不会显示所有代码)“委托(delegate)”EventCallback 被执行,但“索引超出了数组的范围”;发生这种情况是因为访问索引值的代码总是获得最后一个索引 + 1

要解决此问题,请使用应为其分配 i 值的局部变量,例如“

var local = i;

See my answer here...

希望这会有所帮助...

关于c# - Blazor 如何使用绑定(bind)到 bool 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57910886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com