Building HtmlStrings in ASP.NET MVC
Asked Answered
C

5

11

I have an extension method that needs to return an HtmlString. The method has a loop which will build the HtmlString, however the HtmlString object has no Append method and does not allow concatenation using the + operator so I am not sure how I would build the HtmlString.

I'd like to use the StringBuilder but it doesn't have a ToHtmlString method...

Any solutions or patterns for this?

Chaunce answered 26/10, 2011 at 12:40 Comment(5)
I think you should be able to build a normal stringbuilder with the HTML content and then output it wrapped in an @Html.Raw() call, would this solve the problem?Kammerer
Thanks - that worked great. Will draft an answer with code for this solution.Chaunce
No problem, glad it helped, no point re-inventing the wheel if there's a helper for it! :)Kammerer
I prefer the answer by @swapneel as it leaves the door open for adding a RouteValueDictionary parameter to your extension method and have the relevent tag rendered in a MVC'y way. This applies whether you are rendering a single tag or a whole block of HTML.Cushiony
I am not sure if I get this right so I won't post it as an answer, but how about using your StringBuilder but adding an extension method to StringBuilder ToHtmlString?Thorfinn
D
12

I think you want to use TagBuilder. See also Using the TagBuilder Class to Build HTML Helpers like this:

// Create tag builder
var builder = new TagBuilder("img");

// Create valid id
builder.GenerateId(id);

// Add attributes
builder.MergeAttribute("src", url);
builder.MergeAttribute("alt", alternateText);
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

// Render tag
return builder.ToString(TagRenderMode.SelfClosing);
Dent answered 26/10, 2011 at 12:50 Comment(0)
B
34

Why not just build the string in a stringbuilder and then

return MvcHtmlString.Create(sb.ToString());
Benkley answered 26/10, 2011 at 12:58 Comment(0)
D
12

I think you want to use TagBuilder. See also Using the TagBuilder Class to Build HTML Helpers like this:

// Create tag builder
var builder = new TagBuilder("img");

// Create valid id
builder.GenerateId(id);

// Add attributes
builder.MergeAttribute("src", url);
builder.MergeAttribute("alt", alternateText);
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

// Render tag
return builder.ToString(TagRenderMode.SelfClosing);
Dent answered 26/10, 2011 at 12:50 Comment(0)
I
1

You could have a look at the fubu spin-off for creating HTML Tags. Here is a SO question that talks a bit about its usage.

Ite answered 26/10, 2011 at 12:46 Comment(0)
G
1

You could write the ToHtmlString() method yourself as a extension method on StringBuilder.

Gargle answered 26/10, 2011 at 12:47 Comment(0)
C
0

There are several solutions to this including using the TagBuilder, but using Html.Raw() worked very well for me:

public static IHtmlString HtmlMethod(this HtmlHelper htmlhelper, Object object)
{
    var sb = new StringBuilder();
    foreach (var item in object)
    {
        sb.Append(object.outputStr)
    }
    return htmlHelper.Raw(sb.ToString());
}
Chaunce answered 26/10, 2011 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.