简体   繁体   English

Blazor:如何为多个枚举类使用 Select Option Razor 组件?

[英]Blazor: How to Use a Select Option Razor Component for Multiple Enum Classes?

I have a select option component that binds and populates values for a specific enum class .我有一个选择选项组件,用于绑定和填充特定枚举类的值。 However, I want the same component to work for another enum class too.但是,我希望相同的组件也适用于另一个枚举类。 I tried to modify the component and use typeof and dynamic but it did not work.我试图修改组件并使用typeofdynamic但它没有用。 How can I re-use the same select option component for multiple enum classes?如何为多个枚举类重用相同的选择选项组件?

Calling the component in my pages:在我的页面中调用组件:

<SelectEnumComponent @bind-EnumProperty="@driver.DriverType"
                            EnumType="@(typeof(DriverType))"/>

Select Option Component:选择选项组件:

// SelectEnumComponent.razor

    <div class="form-group">
        <select class="form-control" 
                value="@EnumProperty"
                @onchange="OnValueChanged">
            @foreach (var type in Enum.GetValues(EnumType))
            {
                <option value="@type">@type</option>
            }
        </select>
    </div>
    
        @code {
    
            [Parameter]
            public dynamic EnumProperty { get; set; }
    
            [Parameter]
            public EventCallback<dynamic> EnumPropertyChanged { get; set; }
    
            [Parameter]
            public Type EnumType { get; set; }
    
            private Task OnValueChanged(ChangeEventArgs e)
            {
                EnumProperty = Enum.Parse(EnumType, e.Value.ToString(), true); // case insensitive
                return EnumPropertyChanged.InvokeAsync(EnumProperty);
            }
        }

Enum Class - PropertyType:枚举类 - 属性类型:

public enum PropertyType
    {
        [Display(Name = "String")]
        String,
        [Display(Name = "Numeric")]
        Numeric,
        [Display(Name = "Double")]
        Double,
        [Display(Name = "Enumeration")]
        Enumeration,
        [Display(Name = "Boolean")]
        Boolean
    }

Enum Class - DriverType:枚举类 - 驱动程序类型:

public enum DriverType
    {
        [Display(Name = "WEB API")]
        API,
        [Display(Name = "Device")]
        Device,
        [Display(Name = "SSD")]
        SSD
    }

Error:错误:

error CS1503: Argument 2: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback<SharedModels.DriverType>' to 'Microsoft.AspNetCore.Components.EventCallback'

I test with both enums and it can work.I use EnumProperty rather than @bind-EnumProperty in the demo and I'm using .net 5:我用两个枚举进行测试,它可以工作。我在演示中使用EnumProperty而不是@bind-EnumProperty ,我使用的是 .net 5:

razor:剃刀:

      <SelectEnumComponent EnumProperty="@driver.DriverType" EnumType="@(typeof(DriverType))" />
        <SelectEnumComponent EnumProperty="@driver.PropertyType" EnumType="@(typeof(PropertyType))" />
@code {
    [Parameter]
    public Driver driver { get; set; } = new Driver();

}

Driver:司机:

public class Driver
    {
        public DriverType DriverType { get;set; }
        public PropertyType PropertyType { get; set; }

    }

result:结果: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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