Skip to content

Blazor generated onclick events on loop do not work #13427

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

Closed
huto1 opened this issue Aug 25, 2019 · 5 comments
Closed

Blazor generated onclick events on loop do not work #13427

huto1 opened this issue Aug 25, 2019 · 5 comments

Comments

@huto1
Copy link

huto1 commented Aug 25, 2019

the bellow one works perfectly.

<p><button onclick="@(new Action(() => tstmethod("tt")))">test method</button></p>

And this one does not work, which is a big issue. If this does not work, there is no way to do any kind of list output.

        @for (int i = 0; i < 5; i++)
        {
            <p><button onclick="@(new Action(() => tstmethod(i.ToString())))">test method</button></p>
        }

method being used:

void tstmethod(string tst1)
    {
        tst = tst1;
    }

Is there any way to accomplish this? Or will it only be fixed in the next version?

@huto1 huto1 changed the title Generated onclick events on loop do not work Blazor generated onclick events on loop do not work Aug 25, 2019
@vertonghenb
Copy link
Contributor

vertonghenb commented Aug 25, 2019

You're not using the correct syntax, mind the "@" sign before onclick:

<p><button @onclick="@(_ => tstmethod("tt"))">test method</button></p>

or

        @for (int i = 0; i < 5; i++)
        {
            <p><button @onclick="@(new Action(() => tstmethod(i.ToString())))">test method</button></p>
        }

@huto1
Copy link
Author

huto1 commented Aug 25, 2019

@vertonghenb your solution solved the problem. @vertonghenb yes, we do need to assign the loop value to another variable:

 @for (int i = 0; i < 5; i++)
        {
            var a = i;
            <p><button @onclick="@(new Action(() => tstmethod(a.ToString())))">test method</button></p>
        }

There was probably some change in the later version, it would be useful if the at sign had some additional coloring to it.

@huto1 huto1 closed this as completed Aug 25, 2019
@vertonghenb
Copy link
Contributor

@huto1 there is a coloring see the following test:
coloring

@huto1
Copy link
Author

huto1 commented Aug 25, 2019

@vertonghenb It used to be green like that, but not anymore.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants