Car*_*arl 3 c# asp.net asp.net-mvc asp.net-core-mvc tag-helpers
我正在玩MVC 6/ASP.Net vNext中创建自定义标记帮助器 - taghelper工作正常,但有没有办法指定有效的asp属性与标签一起使用,以便它们出现在intellisense中?例如,我希望在视图中添加与我的taghelper标准匹配的标记时,asp-ajax和asp-onsuccess会出现在intellisense列表中:
[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"]);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
<form asp-ajax="true" asp-onsuccess="dothis();">
Run Code Online (Sandbox Code Playgroud)
提前致谢
凭借您现在所拥有的(Attributes = AjaxForm),您将获得asp-ajaxIntelliSense form标签.如果您还想data-onsuccess在form标签上提供IntelliSense,您可以添加另一个TargetElement属性,即:[TargetElement("form", Attributes = "asp-onsuccess")].我想要注意的是,这样添加Attributes只能控制"何时" TagHelper运行.可以这样说:只有在HTML元素上存在这些属性时才会运行.
如果要使用属性的值并提供IntelliSense,则可以添加属性:
public bool AspAjax { get; set; }
[HtmlAttributeName("asp-onsuccess")]
public string AspOnSuccess { get; set; }
Run Code Online (Sandbox Code Playgroud)
这种方法不能控制"何时" TagHelper运行,但提供了一种将信息输入到您的运行中的方法TagHelper.它将使您能够获取其值并将其添加为data-属性.请注意,通过添加AspAjax/ AspOnSuccess作为属性,它们的值不再存在,output.Attributes因此您无需删除它们.
希望这有帮助!
| 归档时间: |
|
| 查看次数: |
552 次 |
| 最近记录: |