102425074
102425074

Reputation: 811

How can I binding to a List in Blazor?

Here is my code:

@page "/"

<button onclick="AddClick">Add</button>
@{ 
    foreach (string i in StringList)
    { 
        <div>@i</div>
    }    
}
@code{
    public List<String> StringList { get; set; }=new List<string>() {"555","666"};
    public void AddClick()
    {
        StringList.Add(DateTime.Now.ToString());
    }
}

When I click the button, it does work yet.

I think it is because I haven't bound the List yet. Meanwhile, how can I bind the List?

Would you please help me? Thank you.

Upvotes: 1

Views: 2279

Answers (1)

Nan Yu
Nan Yu

Reputation: 27538

Try add @ before onclick:

<button @onclick="AddClick">Add</button>

Upvotes: 7

Related Questions