Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TagBuilder InnerHtml in ASP.NET 5 MVC 6

Tags:

It seems to me that there are major breaking changes in TagBuilder as of beta7 with no mention about them in the announcements repo.

Specifically .ToString no longer renders the tagbuilder, it just returns the name of the type. previously we could do things like this inside our HtmlHelper extensions to build up nested html elements:

var li = new TagBuilder("li"); li.AddCssClass("inactive"); var span = new TagBuilder("span"); span.SetInnerText(somestring); li.InnerHtml = span.ToString(); 

.InnerHtml now no longer accepts string because it is now IHtmlContent

but since .ToString() doesn't render the tag this doesn't work either:

li.InnerHtml = new HtmlString(span.ToString()) 

it merely renders as "Microsoft.AspNet.Mvc.Rendering.TagBuilder", the name of the type.

I don't see any new methods on TagBuilder to provide the needed functionality. What am I missing? How can I build complex nested html with TagBuilder now?

like image 874
Joe Audette Avatar asked Sep 05 '15 18:09

Joe Audette


1 Answers

Using MVC 6, at the time of writing, Tagbuiler.InnerHtml has indeed no setter anymore. It has some methods instead to append the element. For instance you could write:

var container = new TagBuilder("div"); var input = new TagBuilder("input");  container.InnerHtml.AppendHtml(input); 
like image 197
Memet Olsen Avatar answered Sep 21 '22 02:09

Memet Olsen