This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
Add methods to TagBuilder to render the tags only #5689
Closed
Description
I am using a TagBuilder
to create tags dynamically in a view. In MVC5 we could render the start and end tags independently with the same tag builder without having to render the content. If the content is part of the razor view it can't be added to the instance of the tag builder as it needs to be set with InnerHtml
.
A solution that I'd like would be to add two methods, RenderStartTag()
and RenderEndTag()
.
I have tried to use the TagRenderMode
without success, it renders the end tag twice.
@{
TagBuilder tag = new TagBuilder("article");
tag.AddCssClass("big");
tag.TagRenderMode = TagRenderMode.StartTag;
}
@tag
<div>MY CONTENT</div>
@{
tag.TagRenderMode = TagRenderMode.EndTag;
}
@tag
It results in
</article>
<div>MY CONTENT</div>
</article>
Ideally I could just write:
@{
TagBuilder tag = new TagBuilder("article");
tag.AddCssClass("big");
}
@tag.RenderStartTag()
<div>MY CONTENT</div>
@tag.RenderEndTag()
Or with a syntax that would capture the razor output:
@using(TagBuilder tag = new TagBuilder("article"))
{
tag.AddCssClass("big");
<div>MY CONTENT</div>
}