【问题标题】:Append data attributes onto a child DOM element, in a tag helper在标签助手中将数据属性附加到子 DOM 元素上
【发布时间】:2019-04-25 10:26:05
【问题描述】:

我有一个标签助手styled-checkbox,它包装了<input type="checkbox"> 并添加了其他元素,以便我获得一个自定义样式的复选框。最终呈现的复选框具有以下 DOM 结构:

<span>
  <input type="checkbox"...>
  <label..>
  <input type="hidden"..>
</span>

为此,我的 TagHelper 更改了 TagName,然后附加 HTML:

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
    output.TagName = "span";
    StringBuilder content = new StringBuilder("");
    content.Append($@"<input type=""checkbox"" value=""true"" name=""{Name}"" ... />");
    ... more content Appending here ...

    output.Content.SetHtmlContent(content.ToString());
}

但是,我在使用标签时添加到标签的任何数据属性都会附加到span。我希望它们改为附加到 input 元素 - 这可能吗?

例如:

<styled-checkbox data-foo="bar"></styled-checkbox>

在该示例中,我希望将“data-foo”属性附加到复选框输入中 - 但我看不到这样做的方法,有可能吗?

【问题讨论】:

    标签: c# asp.net-core tag-helpers


    【解决方案1】:

    您可以使用 TagBuilder 并处理元素的属性。

    [HtmlTargetElement("styled-checkbox")]  
        public class MyCustomTagHelper : TagHelper  
        {  
            public string Name { get; set; }  
            public override void Process(TagHelperContext context, TagHelperOutput output)  
            {  
                output.TagName = "span";  
                output.TagMode = TagMode.StartTagAndEndTag;  
                // collect all attributes of styled-checkbox tag
                var attributes = context.AllAttributes.ToDictionary(a => a.Name, a => a.Value.ToString()); 
    
                var writer = new System.IO.StringWriter();
    
                CreateInputTagBuilder(attributes).WriteTo(writer, HtmlEncoder.Default);
                CreateHiddenInputTagBuilder().WriteTo(writer, HtmlEncoder.Default);
                // clear attributes of styled-checkbox
                output.Attributes.Clear();
                output.Content.SetHtmlContent(writer.ToString());
            }  
            private TagBuilder CreateInputTagBuilder(Dictionary<string,string> attributes)
            {
    
                var inputBuilder = new TagBuilder("input");
                inputBuilder.MergeAttributes(attributes);
    
                // use MergeAttribute instead of Add, Add method throws exception if an attribute exists
                inputBuilder.MergeAttribute("type","checkbox");
                inputBuilder.MergeAttribute("name",this.Name);
    
                return inputBuilder;
            }
            private TagBuilder CreateHiddenInputTagBuilder()
            {
                var inputBuilder = new TagBuilder("input");
                inputBuilder.Attributes.Add("type","hidden");
                return inputBuilder;
            }
        }
    

    cshtml:

    <styled-checkbox data-foo="bar" value="33" name="saeed"></styled-checkbox>
    

    输出:

    <span>
        <input data-foo="bar" name="saeed" type="checkbox" value="33">
        <input type="hidden">
    </span>
    

    【讨论】:

    • 啊哈——我只需要var attributes = context.AllAttributes.ToDictionary(a =&gt; a.Name, a =&gt; a.Value.ToString());——那是缺失的部分,然后我可以将它们添加到我的输入中;)
    猜你喜欢
    • 2019-11-06
    • 1970-01-01
    • 2020-11-10
    • 2011-05-28
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2014-03-10
    • 2016-07-24
    相关资源
    最近更新 更多