@Anders method as an extension method. The nice thing about this is you can append several MvcHtmlStrings together along with other values (eg normal strings, ints etc) as 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));
}
Example call:
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 nicer if we could overload operator+