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
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;
<li> item @i <button onclick=@(() => clickItem(copy))>Click</button> </li>
}
I tried this, and it worked. Hope it seems helpful to you.
@foreach (var item in ListOfUser)
{
<tr>
<td>@item.FirstName</td>
<td>
<button @onclick="(() => GetDetail(item.UserId)) "> Click</button>
</td>
</tr>
}