I\'m trying out Blazor and I\'m getting this exception when I\'m trying to load a list of checkboxes using a for loop.
I\'ve created the list here:
p
You can solve it like this:
@for (int j = 0; j < Months.Count; j++)
{
int i = j; // a local copy to solve the capture problem
}
Which means this is a new, Blazor specific variation on 'capturing the loop variable'.
When you look at the
file you can see that the checkbox requires two lambda's, here is a snippet:
__value => Months[i].isMonthChecked = __value
and after the for-loop, when the RenderTree is being built, the value of i
will be Count for all months.
Using Count()-1
in the original code (your temporary fix) will prevent the exception but note that the form will not function properly, the wrong month will be checked.
General conclusion: don't mix EditForm with a for-loop. foreach()
is safe.