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