@*get the selected value using two-way binding variable*@
@foreach (var _gamesValue in _gamesValues)
{
<p>MultiSelect value is:<strong>@_gamesValue</strong></p>
}
<SfMultiSelect TValue="List<string>" TItem="Games" Placeholder="Favorite Sports" DataSource="@LocalData" @bind-Value="@_gamesValues">
<MultiSelectFieldSettings
Text="Text" Value="ID"></MultiSelectFieldSettings>
</SfMultiSelect>
<button @onclick="@UpdateValue">update the Value</button>
@code {
private List<string> ValidationErrors { get; set; } = new();
//preselect the value
private List<string> _gamesValues { get; set; } = new List<string> { "Game1", "Game4" };
public class Games
{
public string ID { get; set; }
public string Text { get; set; }
}
List<Games> LocalData = new List<Games> {
new Games() { ID= "Game1", Text= "American
Football" },
new Games() { ID= "Game2", Text= "Badminton" },
new Games() { ID= "Game3", Text= "Basketball" },
new Games() { ID= "Game4", Text= "Cricket" },
new Games() { ID= "Game5", Text= "Football" },
new Games() { ID= "Game6", Text= "Golf" },
new Games() { ID= "Game7", Text= "Hockey" },
new Games() { ID= "Game8", Text= "Rugby"},
new Games() { ID= "Game9", Text= "Snooker" },
new Games() { ID= "Game10", Text= "Tennis"},
};
void UpdateValue()
{
//Dynamically update the value
this._gamesValues = new List<string> { "Game7", "Game8" };
}
}
|