asp.net core tags 扩展之 id 和 name

2018-06-22 07:48:01来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

asp.net core 页面 TagHelper  的 Id 和 Name 属性扩展 。

 1     [HtmlTargetElement(Attributes = "asp-name")]
 2     public class NameTagHelper : TagHelper
 3     {
 4         private const string NameAttributeName = "asp-name";
 5 
 6         [HtmlAttributeName(NameAttributeName)]
 7         public ModelExpression Name { get; set; }
 8 
 9         [ViewContext, HtmlAttributeNotBound]
10         public ViewContext ViewContext { get; set; }
11 
12         private IHtmlGenerator _generator;
13 
14         public NameTagHelper(IHtmlGenerator generator)
15         {
16             this._generator = generator;
17 
18         }
19 
20         public override void Process(TagHelperContext context, TagHelperOutput output)
21         {
22             if (context == null)
23             {
24                 throw new ArgumentNullException(nameof(context));
25             }
26             if (output == null)
27             {
28                 throw new ArgumentNullException(nameof(output));
29             }
30 
31             if (this.Name != null)
32             {
33                 if (this.Name.Metadata == null)
34                 {
35                     throw new ArgumentException(nameof(Name));
36                 }
37 
38                 string value = NameAndIdProvider.GetFullHtmlFieldName(ViewContext, this.Name.Name);
39 
40                 output.Attributes.SetAttribute("name", value);
41             }
42         }
43     }
44 
45     [HtmlTargetElement(Attributes = "asp-id")]
46     public class IdTagHelper : TagHelper
47     {
48         private const string IdAttributeName = "asp-id";
49 
50         [HtmlAttributeName(IdAttributeName)]
51         public ModelExpression Id { get; set; }
52 
53         [ViewContext, HtmlAttributeNotBound]
54         public ViewContext ViewContext { get; set; }
55 
56         private IHtmlGenerator _generator;
57 
58         public IdTagHelper(IHtmlGenerator generator)
59         {
60             this._generator = generator;
61 
62         }
63 
64         public override void Process(TagHelperContext context, TagHelperOutput output)
65         {
66             if (context == null)
67             {
68                 throw new ArgumentNullException(nameof(context));
69             }
70             if (output == null)
71             {
72                 throw new ArgumentNullException(nameof(output));
73             }
74 
75             if (this.Id != null)
76             {
77                 if (this.Id.Metadata == null)
78                 {
79                     throw new ArgumentException(nameof(Id));
80                 }
81 
82                 string idFieldName = NameAndIdProvider.GetFullHtmlFieldName(ViewContext, this.Id.Name);
83                 string idFieldValue = NameAndIdProvider.CreateSanitizedId(this.ViewContext, idFieldName, _generator.IdAttributeDotReplacement);
84 
85                 output.Attributes.SetAttribute("id", idFieldValue);
86             }
87         }
88     }

 

原博客链接 : https://blog.wuliping.cn/post/aspnet-core-taghelper-extensions-for-id-and-name

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:VopSdk一个高逼格微信公众号开发SDK:自动化生产(装逼模式开启

下一篇:NancyFx 2.0的开源框架的使用-Stateless