Here's a basic example of removing an item from a list. This is an index page. If you run this, you'll see the items remove from the list when you click the button.
@page "/"
@foreach (var item in _list)
{
<div>@item</div>
}
<p>
@if (_list.Count == 0)
{
<div>All Items Removed. <b @onclick="SetList" style="cursor: pointer;">Reset</b></div>
}
</p>
<button @onclick="RemoveItem">Remove</button>
@code {
private List<int> _list = new();
protected override void OnInitialized()
{
SetList();
}
private void SetList()
{
_list = new List<int> { 10, 20, 30, 40, 50 };
StateHasChanged();
}
private void RemoveItem()
{
var lastIndex = _list.Count - 1;
if (lastIndex >= 0)
_list.RemoveAt(lastIndex);
}
}