blazor variable argument passing to onclick function

后端 未结 2 680
轮回少年
轮回少年 2020-12-12 02:22

I want to pass the int i into the button onclick function for each list item. I expected the \"clickItem\" function will receive 0..2 for correspondig list item. But it come

2条回答
  •  长情又很酷
    2020-12-12 02:50

    This is a classic, but slightly new in the context of Blazor.

    You need to make a copy because otherwise the lambda 'captures' the loop variable. Capturing the copy is OK.

    @for (int i = 0; i < 3; i++)
    {
        int copy = i;
        
  • item @i
  • }

提交回复
热议问题