I am trying to create dynamic controls in MudBlazor. I am able to do that using Dictionary, as you can see in the code below. The problem I am facing is while removing a control. When Test3 is removed, one control goes away from the display but it looks like the ref for Test4 still points to the Text3 object. Wondering what I am doing wrong over here.
@foreach(var value in _newValues)
{
<MudTextField T="string" Label=@value @ref=_refs[value] />
}
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="ButtonOnClick">@ButtonText</MudButton>
@code {
private readonly Dictionary<string, MudTextField<string>> _refs = [];
private List<string> _newValues = [];
public string ButtonText { get; set; } = "Remove a TextField";
protected override async Task OnInitializedAsync()
{
_newValues = new List<string>
{
"Test1",
"Test2",
"Test3",
"Test4"
};;
}
void ButtonOnClick()
{
_newValues = new List<string>
{
"Test1",
"Test2",
"Test4"
};
_refs.Remove("Test3");
ButtonText = "Removed";
StateHasChanged();
}
}
enter image description here
New contributor
AZeus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.