SelectEnumTagHelper

Sample use cases of SelectEnumTagHelper.


Installation

Install via nuget package manager,

            Install-Package LazZiya.TagHelpers -Version 1.0.3
            
Then add LazZiya.TagHelpers to _ViewImports.cshtml
    
            @addTagHelper *, LazZiya.TagHelpers
            

Samples

I will use the below enum to generate sample select list drop down


    public enum WeekDays
        {
            [Display(Name = "Monday")]
            MON,

            [Display(Name = "Tuesday")]
            TUE,

            [Display(Name = "Wednesday")]
            WED,

            [Display(Name = "Thursday")]
            THU,

            [Display(Name = "Friday")]
            FRI,

            [Display(Name = "Saturday")]
            SAT,

            [Display(Name = "Sunday")]
            SUN
        }

Simple dropdown
    
            <select-enum enum-type="typeof(WeekDays)"
                         name="weekDay">
            </select-enum>
        

Dropdown with pre-selected value
    
            <select-enum enum-type="typeof(WeekDays)"
                         name="weekDay"
                         selected-value="(int)WeekDays.FRI">
            </select-enum>
        

Dropdown with localized display names
Select culture:
    
            @using Microsoft.AspNetCore.Mvc.Localization
            @inject IViewLocalizer Localizer

            <select-enum enum-type="typeof(WeekDays)"
                         name="weekDay"
                         selected-value="(int)WeekDays.FRI"
                         text-localizer-delegate="@(delegate(string s) { return Localizer[s].Value; })">
            </select-enum>
        
or if you are using a shred resources culture localizer the delegate can be like follow:
    
            <select-enum enum-type="typeof(WeekDays)"
                         name="weekDay"
                         selected-value="(int)WeekDays.FRI"
                         text-localizer-delegate="@(delegate(string s) { return MyLocalizer.Text(s).Value; })">
            </select-enum>
        
For more complete tuorial about how to setup localiztion see Developing Multicultural Web Application