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

     [HtmlTargetElement(Attributes = "asp-name")]
public class NameTagHelper : TagHelper
{
private const string NameAttributeName = "asp-name"; [HtmlAttributeName(NameAttributeName)]
public ModelExpression Name { get; set; } [ViewContext, HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; } private IHtmlGenerator _generator; public NameTagHelper(IHtmlGenerator generator)
{
this._generator = generator; } public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (output == null)
{
throw new ArgumentNullException(nameof(output));
} if (this.Name != null)
{
if (this.Name.Metadata == null)
{
throw new ArgumentException(nameof(Name));
} string value = NameAndIdProvider.GetFullHtmlFieldName(ViewContext, this.Name.Name); output.Attributes.SetAttribute("name", value);
}
}
} [HtmlTargetElement(Attributes = "asp-id")]
public class IdTagHelper : TagHelper
{
private const string IdAttributeName = "asp-id"; [HtmlAttributeName(IdAttributeName)]
public ModelExpression Id { get; set; } [ViewContext, HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; } private IHtmlGenerator _generator; public IdTagHelper(IHtmlGenerator generator)
{
this._generator = generator; } public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (output == null)
{
throw new ArgumentNullException(nameof(output));
} if (this.Id != null)
{
if (this.Id.Metadata == null)
{
throw new ArgumentException(nameof(Id));
} string idFieldName = NameAndIdProvider.GetFullHtmlFieldName(ViewContext, this.Id.Name);
string idFieldValue = NameAndIdProvider.CreateSanitizedId(this.ViewContext, idFieldName, _generator.IdAttributeDotReplacement); output.Attributes.SetAttribute("id", idFieldValue);
}
}
}

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

最新文章

  1. Protobuf for Python测试保存和读取文件
  2. window.print() 去掉页眉页脚及打印链接【转载】
  3. tableview的重用机制(面试必问)
  4. apscheduler 绿色版
  5. HTTP详解2-请求、响应、缓存
  6. 用CCRenderTexture和BlendFunc制作游戏教学时使用的黑色覆盖层
  7. Generate Parentheses 解答
  8. octave中的一些基本操作
  9. Node.js 模块系统
  10. Leetcode_111_Minimum Depth of Binary Tree
  11. 基础java中的package的命名规则和import的使用
  12. git冲突解决办法合集
  13. nginx使用https协议
  14. [转] Mongoose简要API
  15. Linux进程优先级系统——设置实时进程优先级
  16. canvars 画花
  17. 3-19(晚) require_relative 和 require. === operator的解释。
  18. mysql中字符集和排序规则说明
  19. consul dns 转发配置
  20. Python爬虫框架--pyspider初体验

热门文章

  1. 浅谈Manacher
  2. 2、Monkey简单使用
  3. verilog学习五点经验分享
  4. Azure VM从ASM迁移到ARM(二)
  5. vim修行
  6. Rails、Nginx、Passenger、bundle之间的协作关系
  7. Celery-4.1 用户指南: Application(应用)
  8. 如何修改AWR的retention,interval
  9. myeclipse debug 工具栏不见了
  10. tar命令解压jdk.tar.gz包 报错 gzip: stdin: not in gzip format