The ASP.NET Core 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; }
}
[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;
}
}
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:
<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>
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:
<website-information>
<context version="1.3" copyright="1790" approved tags-to-show="131"/>
</website-information>
In my use case, the website-information element already has many attributes and I want to add one or more separate nested elements.
UPDATE
I have raised this suggestion on the ASP.NET GitHub page to implement this feature for TagHelpers.
website-information
tag helper instead of a singleinfo
parameter? You could nest tag helpers, but you wont be able to force that only<context>
helpers are nested inside the<website-information>
helper – Knockkneewebsite-information
2. If context made more logical sense being a child element 3. If thecontext
properties are logically grouped together 4. You could have multiplecontext
elements. – Buckler