将TagHelpers嵌套在ASP.NET Core MVC 6中
ASP.NET Core
TagHelper documentation提供了以下示例:
public class WebsiteContext { public Version Version { get; set; } public int CopyrightYear { get; set; } public bool Approved { get; set; } public int TagsToShow { get; set; } } [TargetElement("website-information")] public class WebsiteInformationTagHelper : TagHelper { public WebsiteContext Info { get; set; } public override void Process(TagHelperContext context,TagHelperOutput output) { output.TagName = "section"; output.Content.SetContent( $@"<ul><li><strong>Version:</strong> {Info.Version}</li> <li><strong>Copyright Year:</strong> {Info.CopyrightYear}</li> <li><strong>Approved:</strong> {Info.Approved}</li> <li><strong>Number of tags to show:</strong> {Info.TagsToShow}</li></ul>"); output.TagMode = TagMode.StartTagAndEndTag; } } 然后可以在Razor .cshtml中使用它,如下所示: <website-information info="new WebsiteContext { Version = new Version(1,3),CopyrightYear = 1790,Approved = true,TagsToShow = 131 }"/> 这将生成以下HTML: <section> <ul> <li><strong>Version:</strong> 1.3</li> <li><strong>Copyright Year:</strong> 1790</li> <li><strong>Approved:</strong> true</li> <li><strong>Number of tags to show:</strong> 131 </li> </ul> </section> 这是非常丑陋的标记助手语法.有没有办法嵌套另一个标记帮助器并获得完全的智能,以便网站信息的唯一允许子项可以是上下文?见下面的例子: <website-information> <context version="1.3" copyright="1790" approved tags-to-show="131"/> </website-information> 在我的用例中,website-information元素已经有很多属性,我想添加一个或多个单独的嵌套元素. UPDATE 我在ASP.NET GitHub页面上提出了this建议,以便为TagHelpers实现此功能. 解决方法
您当然可以嵌套标记帮助程序,但可能其他选项(如视图组件,部分视图或显示模板)可能更适合OP描述的方案.
这可能是一个非常简单的子标记助手: [HtmlTargetElement("child-tag",ParentTag="parent-tag")] public class ChildTagHelper : TagHelper { public string Message { get; set; } public override void Process(TagHelperContext context,TagHelperOutput output) { // Create parent div output.TagName = "span"; output.Content.SetContent(Message); output.TagMode = TagMode.StartTagAndEndTag; } } 这也可能是另一个简单的父标记助手: [HtmlTargetElement("parent-tag")] [RestrictChildren("child-tag")] public class ParentTagHelper: TagHelper { public string Title { get; set; } public override async Task ProcessAsync(TagHelperContext context,TagHelperOutput output) { output.TagName = "div"; // Add some specific parent helper html var header = new TagBuilder("h1"); header.Attributes.Add("class","parent-title"); header.InnerHtml.Append(this.Title); output.PreContent.SetContent(header); // Set the inner contents of this helper(Will process any nested tag helpers or any other piece of razor code) output.Content.SetContent(await output.GetChildContentAsync()); } } 在剃刀视图中,您可以编写以下内容: <parent-tag title="My Title"> <child-tag message="This is the nested tag helper" /> </parent-tag> 哪个将呈现为: <div> <h1 class="parent-title">My Title</h1> <span>This is the nested tag helper</span> </div> 您可以选择强制标记帮助程序以特定方式嵌套: >在父标记帮助程序中使用[RestrictChildren(“child-tag”,“another-tag”)]来限制允许的嵌套标记帮助程序>在声明子标记帮助程序时强制使用嵌套在特定父标记内的标记[HtmlTargetElement(“child-tag”,ParentTag =“parent-tag”)]时使用ParentTag参数 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – ServiceStack新服务并发ASP.NET MVC网站
- Exchange 2016通过New-mailboxSearch批量删邮件
- asp.net-mvc – 从ASP.NET Web API返回一些字段
- asp.net-mvc – Post Post和Comment应该在Post Aggregate中
- asp.net-mvc – 路由测试ASP.NET MVC4
- asp.net-mvc – HttpContext中需要什么来允许FormsAuthenti
- asp.net-core – 在运行时更改注入的对象
- asp.net-mvc-3 – 将@helper代码转移到App_Code文件夹抛出错
- asp.net-mvc-3 – ASP.Net MVC 3 JSON模型绑定和服务器端模
- asp.net-mvc – 将List参数传递到ASP.NET MVC3中的自定义操