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>
}
I am using a
TagBuilderto 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 withInnerHtml.A solution that I'd like would be to add two methods,
RenderStartTag()andRenderEndTag().I have tried to use the
TagRenderModewithout success, it renders the end tag twice.It results in
Ideally I could just write:
Or with a syntax that would capture the razor output: