• 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

  • 相关阅读:
    WIN7右下角的声音图标不见了
    无法解决 equal to 运算中 "Chinese_PRC_BIN" 和 "Chinese_PRC_CI_AS" 之间的排序规则冲突
    查看表空间信息SQL集合
    Oracle分区表
    Oracle数据库的创建、数据导入导出
    Oracle查询出最最近一次的一条记录
    adb命令
    synergy在Windows和ubuntu 多台PC共享一套键盘鼠标
    git add 之后因为没提交正确文件需要撤销
    make clean-kernel && make kernel
  • 原文地址:https://www.cnblogs.com/passingwind/p/aspnet-core-taghelper-extensions-for-id-and-name.html
Copyright © 2020-2023  润新知