Skip to content

It imposible get index value when iterate over button element with a @onclick function #24460

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
Ksantacr opened this issue Jul 31, 2020 · 4 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@Ksantacr
Copy link

Describe the bug

Is not possible to get index value on iteration over button element

To Reproduce

  1. Open Visual Studio Community 2019
  2. Create Blazor WebAssembly App (with default settings)
  3. On Index.razor after SurveyPrompt component add:
<ol>
    @for (int i = 0; i < list.Count() - 1; i++)
    {
        <li>
            <button @onclick=@(() =>Call(i))>
                item # @i
            </button>
        </li>
    }
</ol>
<button @onclick=AddItem>
    Add item
</button>

@code {
    List<string[]> list = new List<string[]>();
    private void AddItem()
    {
        string[] l = new string[9];
        list.Add(l);
    }
    private void Call(int i)
    {
        Console.WriteLine(i);
    }
}
  1. Click Add Item button, 4 times
  2. Click whatever button with Call (item # 1) function.
  3. I excepted if I click on Item#0 it sends me 0, but it sends the current size of List

Further technical details

  • ASP.NET Core version
    5.0.100-preview.3.20216.6
  • Include the output of dotnet --info
SDK de .NET Core (reflejando cualquier global.json):
 Version:   5.0.100-preview.3.20216.6
 Commit:    9f62a32109

Entorno de tiempo de ejecución:
 OS Name:     Windows
 OS Version:  10.0.18362
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100-preview.3.20216.6\

Host (useful for support):
  Version: 5.0.0-preview.3.20214.6
  Commit:  b037784658

.NET SDKs installed:
  2.1.802 [C:\Program Files\dotnet\sdk]
  2.2.402 [C:\Program Files\dotnet\sdk]
  3.1.100 [C:\Program Files\dotnet\sdk]
  3.1.302 [C:\Program Files\dotnet\sdk]
  3.1.400-preview-015178 [C:\Program Files\dotnet\sdk]
  5.0.100-preview.3.20216.6 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-preview.3.20215.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-preview.3.20214.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-preview.3.20214.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version

Visual Studio Community 2019 Version 16.6.5

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Jul 31, 2020
@mrpmorris
Copy link

This is just the way C# works. The loop variable is not captured so always comes out with the same value.

To solve it either so a foreach over Enumberable.Range, or inside your loop capture the value to a local variable and use that instead.

for (int i, etc)
{
  int currentIndex = i;
  etc
} 

@Ksantacr
Copy link
Author

This is just the way C# works. The loop variable is not captured so always comes out with the same value.

To solve it either so a foreach over Enumberable.Range, or inside your loop capture the value to a local variable and use that instead.

for (int i, etc)
{
  int currentIndex = i;
  etc
} 

Thanks solved, but I need to catch the value inside the loop.

Where I Can find information about this C# issue?

@mrpmorris
Copy link

What is it doing now you've added the suggested change, and what would you like it to do instead?

@mkArtakMSFT mkArtakMSFT added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Aug 3, 2020
@ghost ghost added the Status: Resolved label Aug 3, 2020
@ghost
Copy link

ghost commented Aug 4, 2020

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed Aug 4, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Sep 3, 2020
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants