简体   繁体   中英

Blazor "Work with radio buttons" enum issue

I am trying to implement the "Work with radio buttons" example found at https://learn.microsoft.com/en-us/as.net/core/blazor/forms-validation?view=as.netcore-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

If you want to use foreach loop over enum for radio button, Microsoft has an example https://learn.microsoft.com/en-us/as.net/core/blazor/forms-validation?view=as.netcore-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>

I am trying to implement the "Work with radio buttons" example found at https://docs.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM