views:

325

answers:

1

So I am creating a HtmlHelper extension method and I have run into a problem when using TagBuilder.SetInnerText(). The helper outputs an option tag. Here is the source of the helper:

public static string Option(this HtmlHelper helper, string value, string text, object htmlAttributes) {
        TagBuilder tagBuilder = new TagBuilder("option");

        tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        tagBuilder.MergeAttribute("value", value);
        tagBuilder.SetInnerText(text);

        return tagBuilder.ToString(TagRenderMode.SelfClosing);
    }

In my view I call

<%= Html.Option("value", "text", new { }) %>

but the inner text of the tag does not get set and I am left with

<option value="value"> </option>

Any ideas on why the SetInnerText() is not setting the text correctly?

Thanks.

+1  A: 
return tagBuilder.ToString(TagRenderMode.SelfClosing) - is the problem

It tries to output <option value="" />, where there is nowhere to insert InnerText.

Make it:

return tagBuilder.ToString(TagRenderMode.Normal)
Developer Art
Thank you! I knew I was overlooking something stupid.
T B