【问题标题】:Method that expands into tag-helper扩展为标签助手的方法
【发布时间】:2020-10-04 00:59:30
【问题描述】:

我在cshtml 文件中有以下方法。它只是扩展为两个label 元素。第一个是一个普通的label 元素。然而,第二个使用标签助手:

async Task field(string str)
{
    <label for="@str">@str</label>

    <label asp-for="@str">@str</label>
}

这是我在 cshtml 文件中定义它并调用它一次的方式:

@{ 
    {
        async Task field(string str)
        {
            <label for="@str">@str</label>

            <label asp-for="@str">@str</label>
        }

        await field("abc");
    }
}

如果我在结果中“查看源代码”,我会看到以下内容:

<label for="abc">abc</label>
<label for="str">abc</label>

请注意,@str 参数已正确传递并在第一种情况下使用,但在第二种情况下没有。因此,似乎在这里将参数传递给 tag-helper 变体存在问题。

关于如何解决这个问题的任何建议?

【问题讨论】:

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


    【解决方案1】:

    在我看来,该参数已成功通过 tag-helper 变体。但是标签 asp-for 属性将被呈现为 asp-for ModelExpression 的名称值(str)而不是 ModelExpression 的模型值(abc)的 for 属性。

    根据label taghelper source codes,你可以发现标签助手会调用Generator.GenerateLabel方法来生成标签标签html内容。

    Generator.GenerateLabel有五个参数,第三个参数表达式用于生成标签的for属性。

     var tagBuilder = Generator.GenerateLabel(
            ViewContext,
            For.ModelExplorer,
            For.Name,
            labelText: null,
            htmlAttributes: null);
    

    如果你想显示 for 属性的 str 值,你应该创建一个自定义标签 labeltaghelper。

    更多细节,您可以参考以下代码:

    [HtmlTargetElement("label", Attributes = "asp-for")]
    public class ExtendedAspForTagHelper:LabelTagHelper
    {
        public ExtendedAspForTagHelper(IHtmlGenerator generator)
            : base(generator)
        {
        }
        public override int Order => -10000;
    
    
        //public override void Process(TagHelperContext context, TagHelperOutput output)
        //{
        //    base.Process(context, output);
    
    
    
        //    if (!output.Attributes.TryGetAttribute("maxlength", out TagHelperAttribute maxLengthAttribute))
        //    {
        //        return;
        //    }
    
    
    
        //    var description = $"Only <b>{maxLengthAttribute.Value}</b> characters allowed!";
        //    output.PostElement.AppendHtml(description);
        //}
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
    
    
    
    
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
    
    
    
    
            var tagBuilder = Generator.GenerateLabel(
                ViewContext,
                For.ModelExplorer,
                For.Model.ToString(),
                labelText: null,
                htmlAttributes: null);
    
    
    
    
            if (tagBuilder != null)
            {
                output.MergeAttributes(tagBuilder);
    
    
    
    
                // Do not update the content if another tag helper targeting this element has already done so.
                if (!output.IsContentModified)
                {
                    // We check for whitespace to detect scenarios such as:
                    // <label for="Name">
                    // </label>
                    var childContent = await output.GetChildContentAsync();
                    if (childContent.IsEmptyOrWhiteSpace)
                    {
                        // Provide default label text (if any) since there was nothing useful in the Razor source.
                        if (tagBuilder.HasInnerHtml)
                        {
                            output.Content.SetHtmlContent(tagBuilder.InnerHtml);
                        }
                    }
                    else
                    {
                        output.Content.SetHtmlContent(childContent);
                    }
                }
            }
        }
    }
    

    _ViewImports.cshtml 中引入这个 taghelper

    @addTagHelper *,[yournamespace]
    

    结果:

    【讨论】:

      猜你喜欢
      • 2019-04-09
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2014-11-24
      相关资源
      最近更新 更多