zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    [译]ASP.NET 5 Configuration
    loopback文档翻译
    [译]Testing Node.js With Mocha and Chai
    [译]在Node中使用indicative来验证model
    机器学习:计算机学习西洋跳棋
    OpenCV成长之路(10):视频的处理
    OpenCV成长之路(9):特征点检测与图像匹配
    OpenCV成长之路(8):直线、轮廓的提取与描述
    OpenCV成长之路(7):图像滤波
    OpenCV成长之路(6):数学形态学基本操作及其应用
  • 原文地址:https://www.cnblogs.com/passingwind/p/aspnet-core-taghelper-extensions-for-id-and-name.html
Copyright © 2011-2022 走看看