Ejemplo n.º 1
0
    protected virtual List <SelectListItem> GetSelectItemsFromEnum(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer)
    {
        var localizer = _tagHelperLocalizer.GetLocalizerOrNull(explorer);

        var selectItems = explorer.Metadata.IsEnum ? explorer.ModelType.GetTypeInfo().GetMembers(BindingFlags.Public | BindingFlags.Static)
                          .Select((t, i) => new SelectListItem {
            Value = i.ToString(), Text = GetLocalizedPropertyName(localizer, explorer.ModelType, t.Name)
        }).ToList() : null;

        return(selectItems);
    }
Ejemplo n.º 2
0
        protected virtual List <SelectListItem> GetSelectItemsFromEnum(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer)
        {
            var selectItems    = new List <SelectListItem>();
            var isNullableType = Nullable.GetUnderlyingType(explorer.ModelType) != null;
            var enumType       = explorer.ModelType;

            if (isNullableType)
            {
                enumType = Nullable.GetUnderlyingType(explorer.ModelType);
                selectItems.Add(new SelectListItem());
            }

            var containerLocalizer = _tagHelperLocalizer.GetLocalizerOrNull(explorer.Container.ModelType.Assembly);

            foreach (var enumValue in enumType.GetEnumValues())
            {
                var memberName          = enumType.GetEnumName(enumValue);
                var localizedMemberName = AbpInternalLocalizationHelper.LocalizeWithFallback(
                    new[]
                {
                    containerLocalizer,
                    _stringLocalizerFactory.CreateDefaultOrNull()
                },
                    new[]
                {
                    $"Enum:{enumType.Name}.{memberName}",
                    $"{enumType.Name}.{memberName}",
                    memberName
                },
                    memberName
                    );

                selectItems.Add(new SelectListItem
                {
                    Value = enumValue.ToString(),
                    Text  = localizedMemberName
                });
            }

            return(selectItems);
        }