The @Anders method as an extension method. The best part is that you can add several MvcHtmlStrings along with other values (for example, regular strings, ints, etc.), since ToString is called on each object automatically by the system.
/// <summary> /// Concatenates MvcHtmlStrings together /// </summary> public static MvcHtmlString Append(this MvcHtmlString first, params object[] args) { return new MvcHtmlString(string.Concat(args)); }
Call example:
MvcHtmlString result = new MvcHtmlString(""); MvcHtmlString div = new MvcHtmlString("<div>"); MvcHtmlString closediv = new MvcHtmlString("</div>"); result = result.Append(div, "bob the fish", closediv); result = result.Append(div, "bob the fish", closediv);
It would be much better if we could overload operator +
mike nelson
source share