This code works:
@page "/"
<button @onclick="@(()=>fun(0))" style="color: @style1[0]">
Click me
</button>
<button @onclick="@(()=>fun(1))" style="color: @style1[1]">
Click me
</button>
<button @onclick="@(()=>fun(2))" style="color: @style1[2]">
Click me
</button>
@code{
List<string> style1 = new List<string> { "black" , "black", "black" };
void fun(int m)
{
try
{
if (style1[m] == "black") style1[m] = "red";
else style1[m] = "black";
}
catch
{
Console.WriteLine("m = {0}", m);
}
}
}
But this one does not:
@page "/"
@for (int i = 0; i < 3; i++)
{
<button @onclick="@(()=>fun(i))" style="color: @style1[i]">
Click me
</button>
}
@code{
//same as above
}
and this is the error in Console (without try-catch):
Unhandled exception rendering component: Index was out of range. Must be non-negative and less than the size of the collection.
after try-catch:
m = 3
Thanks