Skip to content

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

Closed
chrdlx opened this issue Jan 30, 2019 · 7 comments
Closed
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@chrdlx
Copy link

chrdlx commented Jan 30, 2019

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

  1. Using the latest ASP.NET CORE 3 PREVIEW 2
  2. Run this code (this is just a modified Counter.cshtml from the template)
@page "/counter"
@for (int c=0;c<stringlist.Count;c++)
{
    <button onclick=@(()=>addText(c))>
        @stringlist[c]
    </button>
}
@functions {
    List<string> stringlist = new List<string>();

    void addText(int index)
    {
        stringlist[index] += "AddedText";
    }

    protected override void OnInit()
    {
        stringlist.Add("STRING1");
        stringlist.Add("STRING2");
        stringlist.Add("STRING3");
        base.OnInit();
    }
}

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:

@for (int c=0;c<stringlist.Count;c++)
{
    int c2 = c; //Intermediate counter
    <button onclick=@(()=>addText(c2))>
        @stringlist[c2]
    </button>
}

This way the loop not only renders correctly, but reacts correctly to the click event modifying the string in the List.

@chrdlx
Copy link
Author

chrdlx commented Jan 30, 2019

Sorry about the code formatting, I'm new here, haven't found a way to make it more readable.

@dmorrison42
Copy link

@chrdlx It's Github Flavored Markdown.

Or you can use the toolbar above the textbox where the <> are.

@muratg muratg added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jan 30, 2019
@dmorrison42
Copy link

dmorrison42 commented Jan 30, 2019

@chrdlx Congrats on your first issue.

This appears to be a duplicate of aspnet/Blazor#764.

My understanding is that in C# for loops the same variable is reused (so all your lambda functions reference the length of your list).

In your workaround you are declaring a new variable each iteration which is the way C# requires lambdas to be written.

@Andrzej-W
Copy link

@chrdlx simple introduction to code formatting
https://github.com/aspnet/Blazor/issues/969

@chrdlx
Copy link
Author

chrdlx commented Feb 1, 2019

@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!

@dmorrison42
Copy link

dmorrison42 commented Feb 1, 2019 via email

@Andrzej-W
Copy link

Hopefully this behaviour will be documented soon:
aspnet/Blazor.Docs#371
I have created a few helpful examples in this issue - you can take a look.

As @dmorrison42 said you can close this issue yourself. You should see the Close issue button to the left of the green Comment button.

@chrdlx chrdlx closed this as completed Feb 1, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

4 participants