Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用HtmlTargetElement(标记帮助程序)中的Attributes属性以一个或另一个标记为目标?_C#_Asp.net Core_Asp.net Core Mvc_Asp.net Core Tag Helpers - Fatal编程技术网

C# 如何使用HtmlTargetElement(标记帮助程序)中的Attributes属性以一个或另一个标记为目标?

C# 如何使用HtmlTargetElement(标记帮助程序)中的Attributes属性以一个或另一个标记为目标?,c#,asp.net-core,asp.net-core-mvc,asp.net-core-tag-helpers,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Core Tag Helpers,我正在努力理解如何在HtmlTargetElement类属性中显示分配给属性的字符串。我有几个问题,我想这些问题会突出我的问题和理解 假设我们只想在make以gm开头并且存在任何模型时激活Html元素。我认为有一种方法可以通过一个类属性(而不是多个)来实现这一点 我正在尝试下面的方法,但这只是一个骗局,不起作用。我很感激这些提示,这样我就可以理解文档所说的这个属性可以采用“类似字符串的查询选择器”的含义了 标记帮助器类 [HtmlTargetElement("auto-price", Attri

我正在努力理解如何在HtmlTargetElement类属性中显示分配给属性的字符串。我有几个问题,我想这些问题会突出我的问题和理解

假设我们只想在make以gm开头并且存在任何模型时激活Html元素。我认为有一种方法可以通过一个类属性(而不是多个)来实现这一点

我正在尝试下面的方法,但这只是一个骗局,不起作用。我很感激这些提示,这样我就可以理解文档所说的这个属性可以采用“类似字符串的查询选择器”的含义了

标记帮助器类

[HtmlTargetElement("auto-price", Attributes = "[make^=gm][model]")]
public class AutoPriceTagHelper : TagHelper
{
还有剃刀

<auto-price make="gm" model="volt" ></auto-price>
<auto-price make="ford" model="mustang"></auto-price>
<auto-price make="ford" ></auto-price>
<auto-price test></auto-price>

它的工作原理与您预期的一样。您缺少的唯一一点是
属性
是一个逗号分隔的属性列表,因此在指定多个属性时,您需要在
属性=“[make^=gm],[model]”中使用逗号

因此,以下是您的助手的模拟版本:

[HtmlTargetElement("auto-price", Attributes = "[make^=gm],[model]")]
public class AutoPriceTagHelper : TagHelper
{
    public string Make { get; set; }
    public string Model { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "ul";
        output.Content.SetHtmlContent(
$@"<li>Make: {Make}</li>
<li>Model: {Model}</li>");
    }
}

谢谢@DanielJG。我还发现“QuerySelected Like”仅限于startwith、endwith、equalto。当这里没有答案时,我用css选择器发布了一个类似的问题。我找不到关于它的任何适当文档,但查看它似乎仅限于那些用途。这表明,在匹配属性值时,只支持完全匹配、前缀和后缀。
<auto-price make="gm" model="volt" ></auto-price>
<auto-price make="ford" model="mustang"></auto-price>
<auto-price make="gmfoo" model="the foo"></auto-price>
<auto-price make="gmbar"></auto-price>
<auto-price test></auto-price>
<ul><li>Make: gm</li>
<li>Model: volt</li></ul>
<auto-price make="ford" model="mustang"></auto-price>
<ul><li>Make: gmfoo</li>
<li>Model: the foo</li></ul>
<auto-price make="gmbar"></auto-price>
<auto-price test=""></auto-price>