-
Notifications
You must be signed in to change notification settings - Fork 10.3k
[Blazor] Bug in the for loop in Razor template #6591
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
@txgz999 Kindly check the generated html code, is the expected argument passed into |
In the first example the for loop is assigning the same variable to all the lambdas so, at the end of the loop, all the lambdas will point to the same value. In the second example the enumerator produces a collection of different variables and the loop binds them to the lambdas so they’ll have different values. Try to create a local variable inside the first loop and assign it to the lambda as suggested in this issue: https://github.com/aspnet/Blazor/issues/1402 |
Thanks for your suggestion Mubarak, but I don't know how to check the generated html code in this case. Right click the button and select "Inspect" from Chrome? I do not see the onclick handler that way. Would you please tell me the steps or a screenshot
On Friday, January 11, 2019, 1:12:04 AM EST, Mubarak Imam <[email protected]> wrote:
@txgz999 Kindly check the generated html code, is the expected argument passed into showNumber?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Thanks for your suggestion Alessandro, and it works. I wish I had found that ticket before posting.
On Friday, January 11, 2019, 1:50:28 AM EST, Alessandro Ghidini <[email protected]> wrote:
In the first example the for loop is assigning the same variable to all the lambdas so, at the end of the loop, all the lambdas will point to the same value. In the second example the enumerator produces a collection of different variables and the loop binds them to the lambdas so they’ll have different values. Try to create a local variable inside the first loop and assign it to the lambda as suggested in this issue: aspnet/Blazor#1402
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
I’m glad it helped! |
Describe the bug
Passing variable to event handler inside a for loop behaves unexpectedly
To Reproduce
Using the latest Visual Studio 2017 Version 15.9.5 and the Blazor template, create a page with the following content:
@page "/forloop"
@for(var i=1; i<=2; i++) {
<button onclick="@(()=>showNumber(i))">@i</button>
}
@functions {
void showNumber(int n) {
Console.WriteLine(n);
}
}
When I click either of these two buttons, number 3 is passed to the event handling function.
Expected behavior
I expect when I click the first button, the value passed to the event handler should be 1, and when clicking the second button, the value passed to the handler should be 2.
Screenshots
Additional context
As a comparison, if I replace the for loop with a foreach loop as follows:
@foreach(var i in Enumerable.Range(1,2)) {
<button onclick="@(()=>showNumber(i))">@i</button>
}
then the behavior is expected.
The text was updated successfully, but these errors were encountered: