asp.net core tags 扩展之 id 和 name

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

posted @ 2017-05-21 14:25  Passingwind  阅读(443)  评论(0编辑  收藏  举报