-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Razor components/Blazor doesn't iterate correctly inside a for loop of a List #7134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Sorry about the code formatting, I'm new here, haven't found a way to make it more readable. |
@chrdlx It's Github Flavored Markdown. Or you can use the toolbar above the textbox where the |
@chrdlx Congrats on your first issue. This appears to be a duplicate of aspnet/Blazor#764. My understanding is that in C# In your workaround you are declaring a new variable each iteration which is the way C# requires lambdas to be written. |
@chrdlx simple introduction to code formatting |
@Andrzej-W Thank you!! I changed the description of the issue, now looks better!, also I've found in your posts that this isn't a bug per se, but how C# behaves in these scenarios with binding and lambdas. Now I don't know how to proceed here, I guess someone will set this as "Not a bug". Thanks again, regards! |
Assuming you agree that this is not a bug, you should be able to close it
yourself.
Even though there is probably nothing to fix here, you brought some
attention to a confusing C# feature and it's relation to Blazor, and
hopefully made the solution more searchable. So thanks for your
contribution!
|
Hopefully this behaviour will be documented soon: As @dmorrison42 said you can close this issue yourself. You should see the |
Describe the bug
I made a simple razor component that just renders a list of strings in buttons. Each button is clickable so when I click one, I add the text "AddedText" to the string inside the button.
This doesn't work since the counter variable always sets to the last position, throwing an out of range exception.
This seems to happen only in the onclick binding, because the list renders correctly in html.
To Reproduce
Expected behavior
I expected the button when clicked, to change the string in the correct position of the List.
FIX
To circumvent this issue, I've found that if I use an intermediate counter variable in the FOR LOOP it works, so here's the FOR LOOP change:
This way the loop not only renders correctly, but reacts correctly to the click event modifying the string in the List.
The text was updated successfully, but these errors were encountered: