Muhammad Rehan Saeed Muhammad Rehan Saeed - 1 year ago 94
ASP.NET (C#) Question

Nesting TagHelpers in ASP.NET 5 MVC 6

The ASP.NET 5 TagHelper documentation gives the following example:

public class WebsiteContext
public Version Version { get; set; }
public int CopyrightYear { get; set; }
public bool Approved { get; set; }
public int TagsToShow { get; set; }

public class WebsiteInformationTagHelper : TagHelper
public WebsiteContext Info { get; set; }

public override void Process(TagHelperContext context, TagHelperOutput output)
output.TagName = "section";
$@"<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;

This can then be used in your Razor .cshtml as follows:

<website-information info="new WebsiteContext {
Version = new Version(1, 3),
CopyrightYear = 1790,
Approved = true,
TagsToShow = 131 }"/>

This will generate the following HTML:

<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>

This is pretty ugly tag helper syntax. Is there some way to nest another tag helper and get full intelli-sense so that the only allowed child of website-information can be context? See example below:

<context version="1.3" copyright="1790" approved tags-to-show="131"/>

In my use case, the website-information element already has many attributes and I want to add one or more separate nested elements.


I have raised this suggestion on the ASP.NET GitHub page to implement this feature for TagHelpers.

Answer Source

You can certainly nest tag helpers, although maybe other options like view components, partial views or display templates might be better suited for the scenario described by the OP.

This could be a very simple child tag helper:

[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.TagMode = TagMode.StartTagAndEndTag;     

And this could also be another simple parent tag helper:

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");

        // 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());            

In a razor view you could then write the following:

<parent-tag title="My Title">
    <child-tag message="This is the nested tag helper" />

Which would be rendered as:

    <h1 class="parent-title">My Title</h1>
    <span>This is the nested tag helper</span>

You can optionally enforce tag helpers to be nested in a particular way:

  • Use [RestrictChildren("child-tag", "another-tag")] in the parent tag helper to limit the allowed nested tag helpers
  • Use the ParentTag parameter when declaring the child tag helper to enforce the tag being nested inside a particular parent tag [HtmlTargetElement("child-tag", ParentTag = "parent-tag")]