Get Items in Blazor ListBox Component
4 Nov 20251 minute to read
The GetDataByValue method returns the data item or items corresponding to the value or values provided, based on the field mapped to Value in ListBoxFieldSettings. If a provided value does not match any item, it is ignored and not included in the result.
The following example demonstrates the GetDataByValue method. In this sample, the Value field is mapped to Text, so the input array must contain the item Text values. The TValue is string[], which indicates a multiple-selection context.
@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor.DropDowns
<SfListBox @ref="ListBoxObj" DataSource="@Vehicles" TValue="string[]" TItem="VehicleData" @bind-Value="@Value">
<ListBoxFieldSettings Text="Text" Value="Text" />
</SfListBox>
<button @onclick="click">Click</button>
@code {
SfListBox<string[], VehicleData> ListBoxObj;
public List<VehicleData> Vehicles = new List<VehicleData> {
new VehicleData { Text = "Hennessey Venom", Id = "Vehicle-01" },
new VehicleData { Text = "Bugatti Chiron", Id = "Vehicle-02" },
new VehicleData { Text = "Bugatti Veyron Super Sport", Id = "Vehicle-03" },
new VehicleData { Text = "SSC Ultimate Aero", Id = "Vehicle-04" },
new VehicleData { Text = "Koenigsegg CCR", Id = "Vehicle-05" },
new VehicleData { Text = "McLaren F1", Id = "Vehicle-06" },
new VehicleData { Text = "Aston Martin One- 77", Id = "Vehicle-07" },
new VehicleData { Text = "Jaguar XJ220", Id = "Vehicle-08" }
};
public class VehicleData
{
public string Text { get; set; }
public string Id { get; set; }
}
public string[] Value = new string[] { "Bugatti Chiron" };
private void click()
{
var Values = ListBoxObj.GetDataByValue(Value);
}
}