Skip to content Skip to sidebar Skip to footer

Unable To Combine Tags Using Tagbuilder

I'm trying to build a combination tag: First tag: * Second tag: (Attributes snipped for bre

Solution 1:

Try like this:

var label = new TagBuilder("label");
label.Attributes.Add("id", "required" + id);
//I get the id earlier fyi - not pertinent fyi for this question )// Create the Spanvar span = new TagBuilder("span");
span.AddCssClass("requiredInidicator");
span.SetInnerText("* ");

//Now combine the span's content with the label tag
label.InnerHtml = span.ToString(TagRenderMode.Normal) + htmlHelper.Encode(labelText);
return MvcHtmlString.Create(label.ToString(TagRenderMode.Normal));

Solution 2:

You shouldn't be calling SetInnertext (especially after having just assigned Innerhtml). You want to use one or the other.

if you're looking to append, keep going with InnerHtml += labeltext.

Solution 3:

You could always do:

stringoutput = "";
output += tabBuilder.ToString(TagRenderMode.Normal);
output += required.ToString(TagRenderMode.Normal);

return MvcHtmlString.Create(output);

The issue could be the TagBuilder assumes one root level HTML tag, but I'm not sure about that.

Solution 4:

How about this:

tagBuilder.SetInnerText(labelText + required.ToString(TagRenderMode.Normal));

Solution 5:

try using the InneHtml property on the span

// Create the Spanvarrequired= new TagBuilder("span");
required.InnerHtml ("SOME TEXT");

Post a Comment for "Unable To Combine Tags Using Tagbuilder"