TagHelper 指定有效属性
TagHelper specify valid attributes
我正在尝试在 MVC 6 / ASP.Net vNext 中创建一个自定义标签助手 - taghelper 工作正常,但是有没有办法指定有效 asp-与标签一起使用的属性,以便它们出现在智能感知中? 例如,我想要 asp-ajax 和 asp-onsuccess 在视图中添加符合我的 taghelper 标准的标签时出现在智能感知列表中:
[TargetElement("form", Attributes = AjaxForm)]
public class UnobtrusiveFormTagHelper : TagHelper
{
private const string AjaxForm = "asp-ajax";
public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);
output.Attributes.Add("data-ajax", true);
output.Attributes.Add("data-onsuccess", context.AllAttributes["asp-onsuccess"]);
}
}
用法:
<form asp-ajax="true" asp-onsuccess="dothis();">
提前致谢
使用您现在拥有的 (Attributes = AjaxForm
),您将在 form
标签的 IntelliSense 中获得 asp-ajax
。如果您还想在 form
标签上的 IntelliSense 中提供 data-onsuccess
,您可以添加另一个 TargetElement
属性,又名:[TargetElement("form", Attributes = "asp-onsuccess")]
。我想指出,像这样添加 Attributes
只会控制 "when" 和 TagHelper
运行。可以把它想象成这样说:只有 运行 当这些属性出现在 HTML 元素上时。
如果您想使用属性的值并提供 IntelliSense,您可以添加属性:
public bool AspAjax { get; set; }
[HtmlAttributeName("asp-onsuccess")]
public string AspOnSuccess { get; set; }
此方法不控制 "when" TagHelper
运行,但提供了一种将信息输入 TagHelper
的方法。它将使您能够获取它们的值并将它们添加为 data-
属性。请注意,通过添加 AspAjax
/AspOnSuccess
作为属性,它们的值将不再存在于 output.Attributes
上,因此您无需删除它们。
希望这对您有所帮助!
我正在尝试在 MVC 6 / ASP.Net vNext 中创建一个自定义标签助手 - taghelper 工作正常,但是有没有办法指定有效 asp-与标签一起使用的属性,以便它们出现在智能感知中? 例如,我想要 asp-ajax 和 asp-onsuccess 在视图中添加符合我的 taghelper 标准的标签时出现在智能感知列表中:
[TargetElement("form", Attributes = AjaxForm)]
public class UnobtrusiveFormTagHelper : TagHelper
{
private const string AjaxForm = "asp-ajax";
public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);
output.Attributes.Add("data-ajax", true);
output.Attributes.Add("data-onsuccess", context.AllAttributes["asp-onsuccess"]);
}
}
用法:
<form asp-ajax="true" asp-onsuccess="dothis();">
提前致谢
使用您现在拥有的 (Attributes = AjaxForm
),您将在 form
标签的 IntelliSense 中获得 asp-ajax
。如果您还想在 form
标签上的 IntelliSense 中提供 data-onsuccess
,您可以添加另一个 TargetElement
属性,又名:[TargetElement("form", Attributes = "asp-onsuccess")]
。我想指出,像这样添加 Attributes
只会控制 "when" 和 TagHelper
运行。可以把它想象成这样说:只有 运行 当这些属性出现在 HTML 元素上时。
如果您想使用属性的值并提供 IntelliSense,您可以添加属性:
public bool AspAjax { get; set; }
[HtmlAttributeName("asp-onsuccess")]
public string AspOnSuccess { get; set; }
此方法不控制 "when" TagHelper
运行,但提供了一种将信息输入 TagHelper
的方法。它将使您能够获取它们的值并将它们添加为 data-
属性。请注意,通过添加 AspAjax
/AspOnSuccess
作为属性,它们的值将不再存在于 output.Attributes
上,因此您无需删除它们。
希望这对您有所帮助!