Katelyn Rochat
Katelyn Rochat

Reputation: 131

Blazorise Select Default Value

Is there a way to choose the default value from a list using the in Blazorise? Right now the default value is null, but I would like it to be say "3".

<Select @bind-SelectedValue="@_someProperty">

                                @foreach (string number in _numbers)
                                    {
                                    <SelectItem Value="@number ">@number </SelectItem>
                                    }
                            </Select>

@code
private static readonly string[] _numbers =
        {
            "1", "2", "3", "4", "5",
        };

Upvotes: 4

Views: 2717

Answers (1)

Ali Kianoor
Ali Kianoor

Reputation: 1243

This is a simple approach to fix this (just add a variable with initial value). You can Also use SelectedValueChanged to manually handle your custom variable.

<Select @bind-SelectedValue="@_selectedProperty" SelectedValueChanged="@OnSelectedValueChanged">

                                @foreach (string number in _numbers)
                                    {
                                    <SelectItem Value="@number ">@number </SelectItem>
                                    }
                            </Select>

@code
{

private string _selectedProperty = 3;
private static readonly string[] _numbers =
        {
            "1", "2", "3", "4", "5",
        };

void OnSelectedValueChanged( string value )
{
    _selectedProperty = value; // Bind to the custom variable
    Console.WriteLine( selectedValue ); // Write in Console Just for Checking
}

}

Upvotes: 5

Related Questions