views:

775

answers:

2

I am just wondering if I have to worry about encoding the values that get output when I use HTML helpers like Html.DropDownList().

If so, how do I encode them? It's easy to do if I were building the drop down manually -- just wrap each value with "Html.Encode()". However, I don't know how to do this when using HTML helpers.

+2  A: 

They do.

If you want to do it yourself it's Html.Encode() and Html.AttributeEncode() depending on where in the HTML you're encoding.

Chad Moran
Deleted my answer. Are you sure they always encode text before rendering html? I'm pretty sure they weren't in the beta...
Will
Answering my own question--yes, they do encode all output in the beta.
Will
Yes they do the source is available at http://codeplex.com/aspnet
Chad Moran
+3  A: 

It looks like the values are encoded automatically, so there's no reason to do it yourself. Here's a snippet from the actual ASP.NET MVC 1.0 source code that you can download from codeplex (in SelectExtensions.cs):

    private static string ListItemToOption(SelectListItem item) {
        TagBuilder builder = new TagBuilder("option") {
            InnerHtml = HttpUtility.HtmlEncode(item.Text)
        };
        if (item.Value != null) {
            builder.Attributes["value"] = item.Value;
        }
        if (item.Selected) {
            builder.Attributes["selected"] = "selected";
        }
        return builder.ToString(TagRenderMode.Normal);
    }
Jim
When using most helpers in ASP.NET MVC it's critical that you do *not* encode text yourself. If you do then you end up with double-encoded text, which is incorrect.
Eilon