gpt4 book ai didi

Blazor "Work with radio buttons"枚举问题

转载 作者:行者123 更新时间:2023-12-04 14:14:27 24 4
gpt4 key购买 nike

我正在尝试实现在 https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1#work-with-radio-buttons 中找到的“使用单选按钮”示例但是我在尝试将它与枚举一起使用时遇到了困难。

@page "/RadioButtonExample"
@using System.ComponentModel.DataAnnotations
@using MyApp.Shared

<h1>Radio Button Group Test</h1>

<EditForm Model="model" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />

@foreach (int status in Enum.GetValues(typeof(Status)))
{
<label>
<InputRadio name="rate" SelectedValue="status" @bind-Value="model.Status" />
@status
</label>
}

<button type="submit">Submit</button>
</EditForm>

<p>You chose: @model.Status</p>

@code {
private Administrator model = new Administrator();

private void HandleValidSubmit()
{
Console.WriteLine("valid");
}
}

我的枚举定义如下
public enum Status
{
Disabled = 0,
Enabled = 1
}

我收到的错误如下所示,我明白为什么我会收到,但是,我不确定如何最好地解决。

TypeInference.CreateInputRadio_0(RenderTreeBuilder, int, int, object, int, TValue, int, TValue, int, EventCallback, int, Expression>)' 不能从用法推断出来。尝试明确指定类型参数。

非常感谢任何帮助

最佳答案

如果你想在单选按钮的枚举上使用 foreach 循环,微软有一个例子
https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-5.0#radio-buttons

<p>
<InputRadioGroup @bind-Value="starship.Manufacturer">
Manufacturer:
<br>
@foreach (var manufacturer in (Manufacturer[])Enum
.GetValues(typeof(Manufacturer)))
{
<InputRadio Value="manufacturer" />
<text>&nbsp;</text>@manufacturer<br>
}
</InputRadioGroup>
</p>

关于Blazor "Work with radio buttons"枚举问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61820763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com