Live Chat Icon For mobile
Live Chat Icon

How can I bind properties to a list in Blazor?

Platform: Blazor| Category: General

We 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 update the concerned item of the list element.

Refer to the following code snippet for further details.

<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

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.