Getting complex data as selected item

Hello,

I have a combobox containing items of a given class like

  public class Country
    {

        [Key]
        public int CountryID { get; set; }

        public string Name { get; set; } = "";

        public string Code { get; set; } = "";

    }

I'm using the combobox like this:

<SfComboBox TValue="int" TItem="Country" PopupHeight="230px" Placeholder="Land auswählen" Value="@ActivePerson.Country.CountryID" DataSource="@AllCountries">
                    <ComboBoxEvents TValue="int" ValueChange="onCountryIDChanged"></ComboBoxEvents>
                    <ComboBoxFieldSettings Text="Name" Value="CountryID"></ComboBoxFieldSettings>
                </SfComboBox>

How can I make to get back the selected Country and not only the id?

Thanks!

1 Reply 1 reply marked as answer

BC Berly Christopher Syncfusion Team June 9, 2020 10:16 AM UTC

Hi Steffen, 
  
Greetings from Syncfusion support.  
  
We would like to inform you that, if you want to get the value of the ComboBox component from the change event arguments, then we need to deserialize the item data and take value and text property from the data. Then, you can use it for your application needs.  Kindly refer the below code example.  
  
@using Syncfusion.Blazor.DropDowns 
@using Newtonsoft.Json 
 
    <SfComboBox TValue="int" TItem="Countries" PopupHeight="230px" Placeholder="Land auswählen" Value="@value" DataSource="@Country"> 
        <ComboBoxEvents TValue="int" ValueChange="onCountryIDChanged"></ComboBoxEvents> 
        <ComboBoxFieldSettings Text="Name" Value="CountryID"></ComboBoxFieldSettings> 
    </SfComboBox> 
 
@code { 
    public int value { get; set; } = 4; 
    public class Countries 
    { 
        public string Name { get; set; } 
 
        public int CountryID { get; set; } 
    } 
 
    List<Countries> Country = new List<Countries> 
{ 
        new Countries() { Name = "Australia", CountryID = 1 }, 
        new Countries() { Name = "Bermuda", CountryID = 2 }, 
        new Countries() { Name = "Canada", CountryID = 3 }, 
        new Countries() { Name = "Cameroon", CountryID = 4 }, 
        new Countries() { Name = "Denmark", CountryID = 5 }, 
        new Countries() { Name = "France", CountryID = 6 }, 
        new Countries() { Name = "Finland", CountryID = 7 }, 
        new Countries() { Name = "Germany", CountryID = 8 }, 
        new Countries() { Name = "Greenland", CountryID = 9 }, 
        new Countries() { Name = "Hong Kong", CountryID = 10 }, 
        new Countries() { Name = "India", CountryID = 11 }, 
        new Countries() { Name = "Italy", CountryID = 12 }, 
        new Countries() { Name = "Japan", CountryID = 13 }, 
        new Countries() { Name = "Mexico", CountryID = 14 }, 
        new Countries() { Name = "Norway", CountryID = 15 } 
    }; 
    public void onCountryIDChanged(ChangeEventArgs<int> args) 
    { 
        var item = JsonConvert.DeserializeObject<Countries>(args.ItemData.ToString()); 
        var value = item.CountryID; 
        var id = item.Name; 
 
    } 
} 

  
Please find the sample from the below link. 
  
Regards, 
Berly B.C 


Marked as answer
Loader.
Up arrow icon