Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

c# - 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 ?

question from:https://stackoverflow.com/questions/65890836/placeholder-for-an-inputselect-containing-enum-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...