10

First of all I'm using MVC 3 RC1 with the Razor view engine. I've got an HTML helper extension which looks like this:

public static string TabbedMenuItem(this HtmlHelper htmlHelper, string text, string actionName, string controllerName) {
    StringBuilder builder = new StringBuilder();
    builder.Append("<li>");

    builder.Append(text);

    builder.Append("</li>");
    return builder.ToString();
}

And on the view it's called like this:

@Html.TabbedMenuItem("Home", "Index", "Home")

The problem I've got is that MVC is automatically HTML encoding the result in the view so all I get is the encoded version of the string:

<li>Home</li>

Does anyone know how to disable the automatic encoding for your HTML helper extensions?

Thanks in advance Andy

AndyM
  • 1,098
  • 1
  • 12
  • 19
  • 1
    For future reference, MVC has a System.Web.Mvc.TagBuilder class that you might want to look into. It doesn't negate the needs for returning IHtmlString, but it comes with more functionality than StringBuilder for this sort of task. – Brian Ball Dec 13 '10 at 18:26

2 Answers2

21
public static IHtmlString TabbedMenuItem(this HtmlHelper htmlHelper, string text, string actionName, string controllerName)
{
    StringBuilder builder = new StringBuilder();
    builder.Append("<li>");

    builder.Append(text);

    builder.Append("</li>");
    return MvcHtmlString.Create(builder.ToString());
}

Use return value IHtmlString. Hope this help.

takepara
  • 10,333
  • 3
  • 33
  • 31
0

Use TagBuilder

Marcelo Mason
  • 6,302
  • 2
  • 33
  • 41