Try this instead:
Counter.razor
@page "/counter"
<h1>Counter</h1>
<button class="btn btn-primary" @onclick="IncrementCount">Add me</button>
<br />
<button class="btn btn-primary" @onclick="Remove">Delete me</button>
<label>Enter row number to delete:</label>
<input type="text" @bind-value="@getKey">
<br />
<label>CollectionCount: @dicCount</label>
<label>Counter count: @currentCount</label>
@{
var dic = MyDic.OrderBy(x => x.Key).ToList();
}
@foreach (var item in dic)
{
<br />
<label>Row Number:</label>
<label>@item.Key</label>
<Child RandomNumber="@item.Value"></Child>
}
@code {
private Dictionary<int, string> MyDic { get; set; } = new Dictionary<int, string>();
public int getKey { get; set; }
private int currentCount = 0;
private int dicCount = 0;
// Instantiate random number generator.
private readonly Random _random = new Random();
private void IncrementCount()
{
MyDic.Add(currentCount, _random.Next().ToString());
dicCount = MyDic.Count;
currentCount++;
}
private void Remove()
{
MyDic.Remove(getKey);
dicCount = MyDic.Count;
currentCount--;
}
}
Child.razor
<h3>Child</h3>
<label>Child Component Loaded</label>
<label>@RandomNumber</label>
@code {
[Parameter]
public string RandomNumber { get; set; }
}
Posted 3 years ago
by selliott
Edited 3 years ago