Unfortunately, this behavior is not built-in. The Html.DropDownList
method (and most other HtmlHelper
methods) always escapes all input text.
Workaround
There are workarounds, however. For example, you could create your own HtmlHelper method that allows unescaped HTML.
But if your needs are as simple as your example, here's a simple workaround:
Use a placeholder, such as |
, and then replace it with
, like this:
@{
var noiceList = new SelectList(new[] { new {ID = "", Name = ""},
new {ID = "y", Name = "Yes|||after3Space"},
"ID", "Name");
}
@Html.Raw(Html.DropDownList("noice", @noiceList).ToString().Replace("|", " "))
Note, you could also create a simple extension method to really reduce the amount of code required:
public static HtmlString Replace(this HtmlString input, string findAll, string replaceWith) {
return new HtmlString(input.ToString().Replace(findAll, replaceWith));
}
This simplifies your Html code to:
@Html.DropDownList("noice", @noiceList).Replace("|", " ")