我想将 int i 传递给每个列表项的按钮 onclick 函数。我预计“clickItem”函数将收到 0..2 对应的列表项。但结果是它总是接收 3 作为参数。似乎 clickItem(i) 中的变量 i 在 for 循环的渲染时没有被评估。我曾尝试将其更改为“clickItem(@i)”,但它仍然相同。我该怎么办?(我使用 blazor 服务器端,.net core 3 preview 5)
@for (int i = 0; i < 3; i++)
{
<li> item @i <button onclick=@(() => clickItem(i))>Click</button> </li>
}
Run Code Online (Sandbox Code Playgroud)
I'm currently trying to call a method from some autogenerated buttons, that are set up from some configuration. So the number of buttons is configurable. I have the blazor page as shown below. My problem is that if I have 3 buttons and I press one then ButtonClicked is called correctly, but the index is always 3 - the end value of int i for the for-statement. I of course want the index of the button to be passed …