Hi,
I have burned some time trying to understand why ComboBox is not selecting predefined value. If myComboData is null/empty initially and loaded from remote API then predefined value is not selected.
Workaround 1: Set _selectedValue to -1 and then back to 2 to trigger selection.
Workaround 2: Wrap combo with if(myComboData != null).
Both seem dirty to me.
In samples (https://demos.telerik.com/blazor-ui/combobox/overview) we don't have a null checks.
<TelerikComboBox @bind-Value=_selectedValue Data="@myComboData" TextField="MyTextField" ValueField="MyValueField" TValue="int" TItem="MyDdlModel">
</TelerikComboBox>
@code {
int _selectedValue { get; set; } = 2; // Preselected value
IEnumerable<MyDdlModel> myComboData { get; set; } = Enumerable.Empty<MyDdlModel>();
protected override async Task OnInitializedAsync()
{
myComboData = await LoadData();
await base.OnInitializedAsync();
}
private async Task<IEnumerable<MyDdlModel>> LoadData()
{
await Task.Delay(1);
return Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
}
public class MyDdlModel
{
public int MyValueField { get; set; }
public string MyTextField { get; set; }
}
}