Hi,
I have a combobox control used to complete a country item in a form, but I'm not able to set the control with the existing country field.
My code:
razor:
@inherits Component
<TelerikComboBox TItem="CountryModel"
TValue="int"
Data="SearchResult"
OnRead="SearchDataAsync"
Filterable="true"
TextField="Name"
ValueField="Id"
@bind-Value="SelectedId"
Width="@Width">
</TelerikComboBox>
razor.cs
using Core.Common.Models;
using Microsoft.AspNetCore.Components;
using Telerik.Blazor.Components;
using Telerik.DataSource;
using WebApp.Client.Services;
namespace WebApp.Client.Components.Views.Common
{
public partial class CountryAutocomplete
{
[Parameter]
public Action<CountryModel> ValueChanged { get; set; }
[Parameter]
public CountryModel Value { get; set; }
[Parameter]
public string Width { get; set; } = "100%";
[Inject]
public StaticDataService StaticDataService { get; set; }
private List<CountryModel> SearchResult { get; set; } = new();
private int _selectedId { get; set; }
private int SelectedId
{
get
{
return _selectedId;
}
set
{
_selectedId = value;
var selected = SearchResult.Single(x => x.Id == value);
ValueChanged?.Invoke(selected);
}
}
protected override async Task OnParametersSetAsync()
{
Console.WriteLine(SelectedId);
if (Value != null && Value.Id > 0 && SelectedId == 0)
{
SearchResult = new List<CountryModel>
{
Value
};
SelectedId = Value.Id;
}
await base.OnParametersSetAsync();
}
private async Task SearchDataAsync(ComboBoxReadEventArgs args)
{
if (args.Request.Filters.Count > 0)
{
var filter = args.Request.Filters[0] as FilterDescriptor;
var search = filter.Value.ToString();
if (search.Length >= 2)
{
SearchResult = await StaticDataService.GetCountriesAsync(search);
args.Data = SearchResult;
}
}
}
}
}
public class CountryModel : Model { public int Id { get; set; } public string Code { get; set; } public string Name { get; set; } public EnumCitizenship Citizenship { get; set; } public override string ToString() { return Name; } }
Value is the country item passed as parameter from a parent component. The control works properly, including ValueChanged callback, except the pre-selecting.
I found another solution. I posted it below.
Thanks!