如何将单选按钮绑定到 属性 这是一个枚举?
How can I bind radio buttons to a property which is an enum?
我正在使用 Blazor 处理单选按钮。一个人的称呼必须有 2 个单选按钮。但是来人的称呼已经很清楚了。因此,例如,如果它是一个男人,我需要在加载页面时选中男人单选按钮。问题是我不能使用 @bind-Value
作为单选按钮。谁能帮帮我?
请根据此示例为您的代码建模:
@foreach (var choice in new[] { Choices.Red, Choices.Green, Choices.Blue })
{
<label>
<input name="yourColor" type="radio"
value="@choice"
checked="@(currentChoice == choice)"
@onchange="@(() => { currentChoice = choice; })">
@choice.ToString()
</label>
}
<p>You chose: @currentChoice</p>
@code {
enum Choices { Red, Green, Blue };
Choices currentChoice = Choices.Red;
}
希望这对您有所帮助...
来源:https://github.com/dotnet/aspnetcore/issues/5579#issuecomment-548061223
因为您特别要求 绑定 解决方案:
到目前为止还没有原生的 Blazor 绑定解决方案...但是项目 Blazorise 为这个问题提供了一个纯绑定解决方案:.
@code{
enum MyEnum
{
A = 0,
B = 1,
C = 2,
D = 3
}
MyEnum checkedValue { get; set; } = MyEnum.B;
}
.razor 文件中的代码:
<p>Current count: @(checkedValue.ToString())</p>
<RadioGroup TValue="MyEnum" Name="group1" @bind-CheckedValue="@checkedValue">
@foreach (var val in Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>()) {
<Radio TValue="MyEnum" Value="@val">@(val.ToString())</Radio>
}
</RadioGroup>
我正在使用 Blazor 处理单选按钮。一个人的称呼必须有 2 个单选按钮。但是来人的称呼已经很清楚了。因此,例如,如果它是一个男人,我需要在加载页面时选中男人单选按钮。问题是我不能使用 @bind-Value
作为单选按钮。谁能帮帮我?
请根据此示例为您的代码建模:
@foreach (var choice in new[] { Choices.Red, Choices.Green, Choices.Blue })
{
<label>
<input name="yourColor" type="radio"
value="@choice"
checked="@(currentChoice == choice)"
@onchange="@(() => { currentChoice = choice; })">
@choice.ToString()
</label>
}
<p>You chose: @currentChoice</p>
@code {
enum Choices { Red, Green, Blue };
Choices currentChoice = Choices.Red;
}
希望这对您有所帮助...
来源:https://github.com/dotnet/aspnetcore/issues/5579#issuecomment-548061223
因为您特别要求 绑定 解决方案:
到目前为止还没有原生的 Blazor 绑定解决方案...但是项目 Blazorise 为这个问题提供了一个纯绑定解决方案:.
@code{
enum MyEnum
{
A = 0,
B = 1,
C = 2,
D = 3
}
MyEnum checkedValue { get; set; } = MyEnum.B;
}
.razor 文件中的代码:
<p>Current count: @(checkedValue.ToString())</p>
<RadioGroup TValue="MyEnum" Name="group1" @bind-CheckedValue="@checkedValue">
@foreach (var val in Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>()) {
<Radio TValue="MyEnum" Value="@val">@(val.ToString())</Radio>
}
</RadioGroup>