zoukankan      html  css  js  c++  java
  • net core 使用tagHelper将 enum枚举类型转换为下拉列表select

        [HtmlTargetElement("enums")]
        //[HtmlTargetElement("enums", TagStructure = TagStructure.WithoutEndTag)]
        public class EnumsTagHelper : TagHelper
        {
            [HtmlAttributeName("asp-enum")]
            public Enum Value { get; set; }
    
    
            [HtmlAttributeName("asp-value")]
            public string SelectedValue { get; set; }
    
    
            [HtmlAttributeName("asp-id")]
            public string Id { get; set; }
    
            [HtmlAttributeName("asp-href")]
            public string DataHref { get; set; }
    
    
            [HtmlAttributeName("asp-valuetype")]
            public int ValueIsIndex { get; set; } = 2;
    
            /// <summary>
            /// 将Enum转换为List<SelectListItem>
            /// </summary>
            /// <returns></returns>
            private List<SelectListItem> GetEnumSelectListItem()
            {
                var list = new List<SelectListItem>();
                var typeInfo = Value.GetType().GetTypeInfo();
                var enumValues = typeInfo.GetEnumValues();
    
                foreach (var value in enumValues)
                {
    
                    MemberInfo memberInfo =
                        typeInfo.GetMember(value.ToString()).First();
                    
    
                    var descriptionAttribute =
                        memberInfo.GetCustomAttribute<DescriptionAttribute>();
    
                    list.Add(new SelectListItem()
                    {
                        Text = descriptionAttribute.Description,
                        Value = (ValueIsIndex == 1) ? ((int)value).ToString() : value.ToString()
                    });
    
                    
                }
                return list;
            }
    
            public override void Process(TagHelperContext context, TagHelperOutput output)
            {
                var list = GetEnumSelectListItem();
                // var value = context.AllAttributes["value"]?.Value as string;
                output.TagName = "select";
                output.Attributes.SetAttribute("id", Id);
                output.Attributes.SetAttribute("data-href", DataHref);
                var content = output.GetChildContentAsync();
                output.Content.AppendHtml(content.Result);
                foreach (var item in list)
                {
                    if (item.Value != null)
                    {
                        if (item.Value == SelectedValue)
                        {
                            output.Content.AppendHtml($"<option value='{item.Value}' selected='selected'>{item.Text}</option>");
                        }
                        else
                        {
                            output.Content.AppendHtml($"<option value='{item.Value}'>{item.Text}</option>");
                        }
                    }
                    else
                    {
                        if (item.Text == SelectedValue)
                        {
                            output.Content.AppendHtml($"<option selected='selected'>{item.Text}</option>");
                        }
                        else
                        {
                            output.Content.AppendHtml($"<option>{item.Text}</option>");
                        }
                    }
                }
                //output.Content.AppendHtml("<select/>");
            }
        }

    更多精彩文章请关注我们的微信公众号FocusDotCore

  • 相关阅读:
    BCD与ASCII码互转-C语言实现
    <<用法
    linux无锁化编程--__sync_fetch_and_add系列原子操作函数
    C中的volatile用法
    c++ k^1
    linux ftp使用相关
    linux应用程序启动时加载库错误问题
    kafka消费者脚本无法启动问题
    Django框架简介
    前端基础之Bootstrap
  • 原文地址:https://www.cnblogs.com/tianfengcc/p/7723491.html
Copyright © 2011-2022 走看看