【问题标题】:From within the tag helper, how can I access the attributes that are not properties of the tag helper?从标签助手中,我如何访问不是标签助手属性的属性?
【发布时间】:2020-02-08 03:55:22
【问题描述】:

如果我有以下标签助手:

using Microsoft.AspNetCore.Razor.TagHelpers;  

namespace CustomTagHelper.TagHelpers  
{  
    [HtmlTargetElement("my-first-tag-helper")]  
    public class MyCustomTagHelper : TagHelper  
    {  
        public string Id { get; set; }
        public string Type { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)  
        {  

            output.SuppressOutput();
            output.Content.Clear();

            output.Content.SetHtmlContent($"<input id={Id} type={Type} />");  
        }  
    }  
}  

在视图中我传递了这个:

<my-first-tag-helper id="my-input" type="text" placeholder="type anything you want" autocomplete="off" disabled="disabled" />

我希望能够访问任何其他添加到该标记但不是标记助手属性的属性,以便我可以将它们添加到输出中。在此示例中,它们将是占位符、自动完成和禁用。

在前面的例子中,结果标签应该是:

<input id="my-input" type="text" placeholder="type anything you want" autocomplete="off" disabled="disabled" />

那么,我如何访问那些不是属性的属性?

【问题讨论】:

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


    【解决方案1】:

    我只是偶然发现了我自己问题的答案。

    传递给名为 Process 的方法的名为 context 的参数包含属性 AllAttributes,顾名思义,它包含所有传递的属性,无论它们是否存在财产与否。因此,您只需过滤掉现有属性,然后创建列表中剩余的属性。

    这就是解决方案的样子:

    using Microsoft.AspNetCore.Razor.TagHelpers;  
    using System.Collections.Generic;
    using System.Linq;
    
    namespace CustomTagHelper.TagHelpers  
    {  
        [HtmlTargetElement("my-first-tag-helper")]  
        public class MyCustomTagHelper : TagHelper  
        {  
            public string Id { get; set; }
            public string Type { get; set; }
    
            public override void Process(TagHelperContext context, TagHelperOutput output)  
            {  
                output.SuppressOutput();
                output.Content.Clear();
    
                // ------------------- Begin Extra Attributes --------------------
    
                var attributeObjects = context.AllAttributes.ToList();
    
                var props = this.GetType().GetProperties().Select(p => p.Name);
    
                attributeObjects.RemoveAll(a => props.Contains(a.Name));
    
                var extraAttributeList = new List<string>();
    
                foreach (var attr in attributeObjects)
                {
                    extraAttributeList.Add($"{attr.Name}=\"{attr.Value}\"");
                }                                                                                                 
    
                // ------------------- End Extra Attributes --------------------
    
                output.Content.SetHtmlContent($"<input id={Id} type={Type} {string.Join(" ", extraAttributeList)}/>");
            }  
        }  
    }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-26
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多