9

How can I use the values of an enum class as options for an InputSelect?

Example enum:

public enum Test
{
    Test1,
    Test2
}

I am using with Blazor with Razor components.

3
  • Do you always accept an answer on a count of the user's name ? Commented Jul 19, 2020 at 21:00
  • I appreciate all answers. I selected the one I did because if was the most focused one, concentrating of how to use the values of an enum class as options. Yours goes beyond that, which might help some people more than the selected answer, but the selected answer is more focused. is that what you mean? Commented Jul 19, 2020 at 21:09
  • 1
    meziantou.net/… Commented Nov 1, 2021 at 3:37

4 Answers 4

22

Here's a working sample how to use enum in InputSelect component:

<EditForm EditContext="@EditContext">
    <DataAnnotationsValidator />

    <div class="form-group">
        <label for="name">Enter your Name: </label>
        <InputText Id="name" Class="form-control" @bind-Value="@comment.Name"></InputText>
        <ValidationMessage For="@(() => comment.Name)" />

    </div>
    <div class="form-group">
        <label for="body">Select your country: </label>

        <InputSelect @bind-Value="@comment.Country" >
          
            @foreach (var country in Enum.GetValues(typeof(Country)))
            {
            
                <option value="@country">@country</option>
            }
        </InputSelect>
               
        <ValidationMessage For="@(() => comment.Country)" />
    </div>

    <p>
        <button type="submit">Submit</button>
    </p>
</EditForm>
   
@code
    {
    private EditContext EditContext;
    private Comment comment = new Comment();

   
    protected override void OnInitialized()
    {
        EditContext = new EditContext(comment);


        base.OnInitialized();
    }

    public enum Country
    {
        USA = 1,
        Britain,
        Germany,
        Israel

    }
    public class Comment
    {

        public string Name { get; set; }
        public Country Country { get; set; }
    }
    
}

Hope this helps...

Sign up to request clarification or add additional context in comments.

Comments

7

So in short it's like this:

<InputSelect @bind-Value="@YourEnum">
    @foreach (var value in Enum.GetValues<YourEnumType>()) {
        <option value="@value">@value</option>
    }
</InputSelect>

Comments

3
<select>

    @foreach (var Item in Enum.GetValues(typeof( DayOfWeek)))
    {
        <option value="@Item">@Item</option>
    }

2 Comments

Although this is an answer if one were to use traditional razor syntax and MVC, the question is to use an InputSelect in Blazor. Im upvoting the answer to enhet instead. IMHO enhet's answer should perhaps be marked as the correct answer instead.
This is not the correct answer as it steers anyone coming to this Q/A later to believe you should use select instead of InputSelect. While it works, you lose all the benefits of using a Blazor Input component in the Edit Context. I got here because and commented because someone referred to this question at a much later date.
0

@enet answer is correct.

To add to this, sometimes enum values won't accept specific chars. you can edit the display of enum value to display specific chars in the code if you have filtered or changed enum values to store correctly. like below in my enum i replace all spaces with underscore. so you can add this to the code to display the enum values on the dropdown correctly.

<InputSelect @bind-Value="@updateUserDetails.Nationality" class="dropDownSelectList">
    @foreach (var countryName in Enum.GetValues(typeof(Nationality)))
        {
         <option value="@countryName">@(@countryName.ToString().Replace("_", " ").Replace("HH", "-"))</option>
        }
</InputSelect>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.