MVC Core 使用TagHelper扩展几个插件

未完待续...

 

案例1:按钮权限校验,无权限不显示,利用TagHelper控制元素是否输出

    [HtmlTargetElement("tc-permission", TagStructure = TagStructure.NormalOrSelfClosing)]
    public class TCPermissionTagHelper : TagHelper
    {
        private IPermissionContext permissionContext = null;
        public TCPermissionTagHelper(IPermissionContext permissionContext)
        {
            this.permissionContext = permissionContext;
        }

        public string[] Permissions { get; set; }
        public string Permission { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            output.TagName = null;
            if (!string.IsNullOrEmpty(Permission))
            {
                Permissions = Permission.SplitToArray<string>();
            }
            //HasPermission:根据UserID、按钮权限编码判断用户是否有此按钮权限,如果无权就不输出
            if (Permissions == null || Permissions.Length == 0 || !permissionContext.HasPermission(Permissions))
            {
                output.SuppressOutput();
                return;
            }

        }
    }

页面中添加:

"@Permissions.系统模块.账户管理.添加">
            "btnAdd" class="btn  btn-primary" onclick="cache.add(); return false;">添加
        

 lable-input插件:

 1     [HtmlTargetElement("label-input")]
 2     public class FieldTagHelper : TagHelper
 3     {
 4         public string FieldName { get; set; }
 5         public string Label { get; set; }
 6         public string Value { get; set; }
 7         public string PlaceHolder { get; set; }
 8         public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
 9         {
10             output.TagName = "div";
11             output.Attributes.Add("class", "form-group");
12             output.Content.SetHtmlContent(
13                 $@"
14                    ");
15 
16 
17             return base.ProcessAsync(context, output);
18         }
19     }

Enum版Select:

 1     /// 
 2     /// Enum版Select
 3     /// 
 4     [HtmlTargetElement("select-enum")]
 5     public class SelectEnumTagHelper : TagHelper
 6     {
 7         public Type EnumType { get; set; }
 8         public object Value { get; set; }
 9         public string Placeholder { get; set; }
10         //public Dictionary Attributes { get; set; }
11         public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
12         {
13             output.TagName = "select";
14             StringBuilder buffer = new StringBuilder();
15             if (!string.IsNullOrWhiteSpace(this.Placeholder))
16             {
17                 buffer.Append($"");
18             }
19             string selected = string.Empty;
20             if (EnumType != null && EnumType.IsEnum)
21             {
22                 var enumFields = Enum.GetValues(EnumType);
23                 foreach (var enumField in enumFields)
24                 {
25                     selected = (int)Value == (int)enumField ? "selected='selected'" : "";
26                     buffer.Append($"");
27                 }
28             }
29             output.Content.SetHtmlContent(buffer.ToString());
30 
31             return base.ProcessAsync(context, output);
32         }
33     }

Source版select:

 1     /// 
 2     /// Source版Select
 3     /// 
 4     [HtmlTargetElement("select-source")]
 5     public class SelectSourceTagHelper : TagHelper
 6     {
 7         public string ValueFieldName { get; set; }
 8         public string TextFieldName { get; set; }
 9 
10         public IEnumerable<object> Source { get; set; }
11         public object Value { get; set; }
12         public string Placeholder { get; set; }
13         //public Dictionary Attributes { get; set; }
14         public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
15         {
16             output.TagName = "select";
17             StringBuilder buffer = new StringBuilder();
18             if (!string.IsNullOrWhiteSpace(this.Placeholder))
19             {
20                 buffer.Append($"");
21             }
22             string selected = string.Empty;
23             if (this.Source != null && this.Source.Count() > 0)
24             {
25                 Type sourceType = Source.FirstOrDefault().GetType();
26                 var tProperty = sourceType.GetProperty(this.TextFieldName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase);
27                 var vProperty = sourceType.GetProperty(this.ValueFieldName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase);
28                 if (tProperty != null && vProperty != null)
29                 {
30                     object text = null;
31                     object value = null;
32                     foreach (var item in Source)
33                     {
34                         text = tProperty.GetValue(item, null);
35                         value = vProperty.GetValue(item, null);
36 
37                         selected = (int)this.Value == (int)value ? "selected='selected'" : "";
38                         buffer.Append($"");
39                     }
40                     output.Content.SetHtmlContent(buffer.ToString());
41                 }
42             }
43 
44             return base.ProcessAsync(context, output);
45         }
46     }
 1 @using MVCCoreDemo
 2 @addTagHelper *,MVCCoreDemo
 3 @{
 4     List personList = new List {
 5     new Person{ ID=1,Name="zhao"},
 6     new Person{ ID=2,Name="qian"},
 7     new Person{ ID=3,Name="sun"},
 8     new Person{ ID=4,Name="li"}
 9     };
10 
11 }
12 
13 "UserName" label="用户名:" placeholder="请输入用户名" value="zhangsan">
14 <select-enum id="select1" name="select1" class="c1" enum-type="@typeof(ECustomerLevel)" Value="3" placeholder="请选择">select-enum>
15 <select-source id="select2" name="select2" class="c1" source="@personList" Value="3" value-field-name="ID" text-field-name="Name" placeholder="请选择">select-source>

 

你可能感兴趣的:(MVC Core 使用TagHelper扩展几个插件)