Reputation: 798
I am trying to implement the "Work with radio buttons" example found at https://learn.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1#work-with-radio-buttons however I ran into difficulty when trying to use it with an enum.
@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");
}
}
My enum is defined as below
public enum Status
{
Disabled = 0,
Enabled = 1
}
The error I receive is the below which I understand why I'm getting, however, I'm not sure how best to resolve.
TypeInference.CreateInputRadio_0(RenderTreeBuilder, int, int, object, int, TValue, int, TValue, int, EventCallback, int, Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Any help much appreciated
Upvotes: 5
Views: 4767
Reputation: 146
If you want to use foreach loop over enum for radio button, Microsoft has an example https://learn.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> </text>@manufacturer<br>
}
</InputRadioGroup>
</p>
Upvotes: 6