InputSelect

<InputSelect @bind-Value="Employee.DepartmentId">
    @foreach (var dept in Departments)
    {
        <option value="@dept.DepartmentId">@dept.DepartmentName</option>
    }
</InputSelect>
<InputSelect @bind-Value="Employee.Gender">
    @foreach (var gender in Enum.GetValues(typeof(Gender)))
    {
        <option value="@gender">@gender</option>
    }
</InputSelect>

Enum

public enum Gender
{
    Male,
    Female,
    Other
}

Razor Page

<EditForm Model="@_product">
    <InputSelect @bind-Value="_product.Gender">
        @foreach (var gender in Enum.GetValues(typeof(Gender)))
        {
            <option value="@gender">@gender</option>
        }
    </InputSelect>
</EditForm>

@code {

    public enum Gender
    {
        Male,
        Female,
        Other
    }

    class Product
    {
        public int ItemId { get; set; }
        public string Name { get; set; }
        public DateTime UpdatedDate { get; set; }
        public string Gender { get; set; }
    }

    private Product[] products;
    private Product _product = new Product();

}

Sources:

https://www.pragimtech.com/blog/blazor/bind-input-select-element-to-enum-in-blazor/

Last modified: November 22, 2020

Author

Comments

Write a Reply or Comment