Skip to content

for loop only render last value #15633

Closed
Closed
@lakani

Description

@lakani

i've modified a littel the counter page to try how i can render array of buttons, but i got a strange behavior
@page "/counter"

Counter

`

<p>Current count: @currentCount</p>

<button class="btn btn-primary" onclick="@IncrementCount">Click me</button>

<button class="btn btn-primary" onclick="@Reset">Reset</button>

@for (int i = 0; i < _Sarr.Length; i++) {
<button class="btn btn-primary" onclick="@(e => bReset( i ))">@_Sarr[i]</button>
}

@functions {
int currentCount = 0;

string[] _Sarr = new string[] { "one", "two", "three" };

void IncrementCount()
{
	currentCount++;
}

void bReset(int s)
{
	Console.WriteLine(s.ToString());
	currentCount += s;


}

void Reset()
{
	currentCount = 0;
}

}
`

but whenever i click on one of the buttons in the array , s variable passed by 3

am i doing someting wrong ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions