-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Strange behavior in events #16410
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
As far as I can see, only your working solution turns the lambda function invoking Does that help? |
I had already tried with
but it does not work.. the only way is to turn it into a string. |
Hmm, I tried that and it worked: @for (var page = 0; page <= 5; page++)
{
var helper = page;
<button @onclick(() => InvokePageChanged(helper))>@(helper)</button>
}
@functions {
void InvokePageChanged(int page)
{
Console.WriteLine(page);
}
} I also expected it to work because the generated code looks like that: ...
for (var page = 0; page <= 5; page++)
{
var helper = page;
...
builder.AddAttribute(13, onclick(() => InvokePageChanged(helper)));
...
}
... |
Yes, I also just tried that and confirmed it works OK. There's no need to do the conversion to string and back again. The necessity to declare a variable inside your loop is just how C# lambdas work - they bind to a variable, not to the value that was in the variable when the lambda was constructed. The StackOverflow that @rstropek linked to is a good source of info about this. Also note how it's different if you use |
Example:
The value of "Page" in InvokePageChanged is always "LastPage".
The only way to make it work is:
The text was updated successfully, but these errors were encountered: