You can bind properties to a list by binding every value in the list to each list element using a
“for” loop. Then if the property gets updated in the list, the HTML DOM will be updated in the concerned item of the list element.
Refer to the code sample.
<h1>Bind List</h1>
<button class="btn btn-primary" @onclick="Generate">Generate and Bind to List</button>
@if (list != null)
{
@foreach (var item in list)
{
<li>@item</li>
}
}
@code {
List<int> list;
const int cMaxNumbers = 10;
protected void Generate()
{
list = new List<int>(cMaxNumbers);
for (int i = 0; i < cMaxNumbers; i++)
{
list.Add(i);
}
}
}
You can download the reference sample here.
Share with