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

  • 相关阅读:
    程序报错怎么进入调试
    va01 无定价过程能被确定
    维护销售区域数据
    SAP VA01 消息 没有用于售达方 XXXXXX 的客户主记录存在
    SAP常见问题与解决方法
    客户没有对功能定义,合作伙伴
    SAP名词解释-售达方|送达方
    英语-20210312
    配置预付款-科目对应关系FBKP
    unzip 解压指定的文件夹或文件 到指定的目录
  • 原文地址:https://www.cnblogs.com/passingwind/p/aspnet-core-taghelper-extensions-for-id-and-name.html
Copyright © 2011-2022 走看看