简体   繁体   中英

Placeholder for an InputSelect containing Enum values

I have an InputSelect that display various options based on an Enum type, as such

<InputSelect @bind-Value=m_ProjectExtended.EstimateType class="form-control form-control-sm">
    @foreach (var type in @GetEnumValues<EstimateType>())
    {
        <option value="@type.Value">@type.Key</option>
    }
</InputSelect>

public Dictionary<string, int> GetEnumValues<T>()
{
    Dictionary<string, int> values = new Dictionary<string, int>();

    foreach (var enumValue in Enum.GetValues(typeof(T)))
    {
        values.Add(enumValue.GetDisplayAttribute(), (int)enumValue);
    }

    return values;
}

What I'm missing is to be able to have an placeholder option selected by default (ie: "Select the option" when creating a new record for example) and also, being able to reset the InputSelect to this placeholder from the component's code.

The way I do it in Angular is that I add the placeholder option with a value of -1 set the binded field to that value, which does the trick.

Problem is that I don't have a value of -1 in the actual C# enums and don't really want one.

Any way to achieve this?

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