TagBuilder is a nice implementation for build HTML elements. But -some- HTML elements can have another elements (I called like children). I could not find any class from Mvc classes.
Question; Should I implement few classes (TagBuilderTree, and TagBuilderNode) which support nested tags or did I miss something?
You can build the child elements in separate TagBuilders and put their generated HTML in the parent TagBuilder.
Here's an example: A <select>
with some <option>
s (example de-fatted for terseness)
TagBuilder select = new TagBuilder("select");
foreach (var language in languages) // never ye mind about languages
{
TagBuilder option = new TagBuilder("option");
option.MergeAttribute("value", language.ID.ToString());
if (language.IsCurrent)
{
option.MergeAttribute("selected", "selected");
}
option.InnerHtml = language.Description;
// And now, the money-code:
select.InnerHtml += option.ToString();
}
OK, I decided to do a little test in my own code base.
I compared these two methods to create the same exact final HTML:
Manually generating the html using a StringBuilder:
var sb = new StringBuilder();
sb.AppendLine("<div class='control-group'>");
sb.AppendFormat(" <label class='control-label' for='{0}_{1}'>{2}</label>", propObj.ModelType, propObj.ModelProperty, propObj.LabelCaption);
sb.AppendLine(" <div class='controls'>");
sb.AppendFormat(" <input id='{0}_{1}' name='{0}[{1}]' value='{2}' />", propObj.ModelType, propObj.ModelProperty, propObj.PropertyValue);
sb.AppendLine(" </div>");
sb.AppendLine("</div>");
return new HtmlString(sb.ToString());
Using multiple TagBuilders and merging the content:
TagBuilder controlGroup = new TagBuilder("div");
controlGroup.AddCssClass("control-group");
TagBuilder label = new TagBuilder("label");
label.AddCssClass("control-label");
label.InnerHtml = propObj.LabelCaption;
TagBuilder controls = new TagBuilder("div");
TagBuilder input = new TagBuilder("input");
input.Attributes["id"] = propObj.ModelType + "_" + propObj.ModelProperty;
input.Attributes["name"] = propObj.ModelType + "[" + propObj.ModelProperty + "]";
input.Attributes["value"] = propObj.PropertyValue;
controls.InnerHtml += input;
controlGroup.InnerHtml += label;
controlGroup.InnerHtml += controls;
return new HtmlString(controlGroup.ToString());
To me, #1 is easier to read and much more concise, but I can appreciat the structure of #2 also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With