Just wondering what the difference is between these two and what the benefit for one or the other of them would be when I build my table in my HtmlHelper
HtmlTable table = new HtmlTable();
and:
TagBuilder table = new TagBuilder("table");
This is more or less the same as this question,
Why use TagBuilder instead of StringBuilder?
but I’m wondering more specifically about the difference between these two .
The main difference is that
HtmlTableprovides typed and correctly named properties for all the valid HTML attributes of the<table>element (e.g.Width,Height,CellSpacing, etc). It also has theRowsproperty which is a typed collection ofHtmlTableRowobjects which each reresent a<tr>element.TagBuilderis a much more generic API which could certainly be used to construct an HTML<table>but you’d need to do more work in a less type safe and less easy to read way.One concrete example where
HmlTablehelps in a way thatTagBuilderdoes not is in the setting of thewidth=""attribute on the<table>element.With
HtmlTable:With
TagBuilder:Note that with
TagBuilderboth the name of the element,table, and the name of the attribute,width, are strings which introduce two opportunities for error (misspellings) that do not occur when usingHtmlTable.