2

有没有办法使用 Blazorise 从列表中选择默认值?现在默认值为空,但我希望它是“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",
        };
4

1 回答 1

3

这是解决此问题的一种简单方法(只需添加一个具有初始值的变量)。您还可以使用 SelectedValueChanged 手动处理您的自定义变量。

<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
}

}
于 2020-06-13T17:23:39.270 回答