【问题标题】:Optional attributes in tag helpers标签助手中的可选属性
【发布时间】:2016-01-17 21:13:38
【问题描述】:

我有一个标签助手,如下所示:

[HtmlTargetElement("foo", Attributes = "bar")]
public class FooTagHelper : TagHelper

[HtmlAttributeName("bar")]
public bool Bar { get; set; }

当我将以下内容添加到视图时,标签助手会按预期处理目标:

<foo bar="true"></foo>

但是,我想做的是使 bar 可选,例如&lt;foo&gt;&lt;/foo&gt; 如果已经取消,我希望它默认为false

这可能吗? HtmlTargetElementAttribute.Attributes 属性的此源代码注释似乎表明不是:

// 总结:
HTML 元素必须包含的属性名称的逗号分隔 System.String
供 Microsoft.AspNet.Razor.TagHelpers.ITagHelper 运行。 * 在......的最后 属性名称充当前缀匹配。

【问题讨论】:

    标签: asp.net-mvc razor asp.net-core-mvc tag-helpers


    【解决方案1】:

    您可以将“bar”从必需属性中删除。

    您可以通过重写 Process 方法并检查属性是否存在来做到这一点。如果没有,请使用其名称和值添加 Bar 属性。您可以将值显式设置为false,但无论如何属性 Bar 默认为 false。

    [HtmlTargetElement("foo")]
    public class FooTagHelper : TagHelper
    {
        [HtmlAttributeName("bar")]
        public bool Bar { get; set; }
    
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (!output.Attributes.ContainsName(nameof(Bar)))
            {
                output.Attributes.Add(nameof(Bar), Bar);
            }
        }
    }
    

    干杯!

    如果您还没有这样做,我建议您查看https://docs.asp.net/projects/mvc/en/latest/views/tag-helpers/index.html 此处提供的文档。

    【讨论】:

    • 标签助手不会处理 recaptcha 元素句号... Process() 永远不会被调用。
    • 这应该可以正常工作,并且已经将 Bar 变成了一个可选属性。它将&lt;foo&gt;&lt;/foo&gt; 渲染为&lt;foo bar="False"&gt;&lt;/foo&gt;。也许您真正使用的标签助手中还有另一个问题?
    • 当我尝试这个时,断点只有在使用标签助手时也包含该属性时才会命中。例如使用您的示例,只有当我像这样使用它时它才会起作用:。是否可以根本不必包含该属性?
    【解决方案2】:

    如果你不想检查可选属性是否在Html中明确指定,你可以检查context.AllAttributes,你不会在上一篇文章中指定的output.Attributes中找到它。

    我注意到,如果我在 Html 中留下(可选)属性 OnlyUrlOfMenuItem,我的 TagHelper 不会呈现。我只需要从上面Mat Hellums 所述的必需属性HtmlTargetElement() 列表中删除属性OnlyUrlOfMenuItem。由于我不希望该属性出现在最终输出中,因此我不需要像上一篇文章中所述添加带有output.Attributes.Add() 的属性。

    这是我的代码,带有可选属性OnlyUrlOfMenuItem,默认值为false

    [HtmlTargetElement("a", Attributes = "MenuItem, LangCode")]
    public class ATagHelper : TagHelper
    {
        readonly IFhpMenuProvider _fhpMenuProvider;
    
        public ATagHelper(IFhpMenuProvider fhpMenuProvider)
        {
            _fhpMenuProvider = fhpMenuProvider;
            OnlyUrlOfMenuItem = false;
        }
    
        [HtmlAttributeName("LangCode")]
        public string LangCode { get; set; }
    
        [HtmlAttributeName("MenuItem")]
        public string MenuItemKey { get; set; }
    
        [HtmlAttributeName("OnlyUrlOfMenuItem")]
        public bool OnlyUrlOfMenuItem { get; set; }
    
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            MenuItem menuItem = _fhpMenuProvider.GetMenuItem(MenuItemKey, LangCode);
            if (menuItem != null)
            {
                ...
                if (string.IsNullOrEmpty(menuItem.Tooltip) == false && OnlyUrlOfMenuItem == false)
                {
                    output.Attributes.SetAttribute("title", menuItem.Tooltip);
                }
                if (string.IsNullOrEmpty(menuItem.Caption) == false && OnlyUrlOfMenuItem == false)
                {
                    output.Content.SetContent(menuItem.Caption);
                }
            }
            base.Process(context, output);
        }
    

    【讨论】:

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