9

How can I get my select list to render without HTML coding

@{
    var noiceList = new SelectList(new[] { new {ID = "", Name = ""}, 
                                   new {ID = "y", Name = "Yes   after3Space"}, 
                 "ID", "Name"); 
 }
 @Html.DropDownList("noice", @noiceList )

rendered

..
<option value="y">Yes&amp;nbsp;&amp;nbsp;&amp;nbsp;3Space</option>
...

How do I get it to instead render

<option value="y">Yes&nbsp;&nbsp;&nbsp;after3Space</option>
Joakim
  • 513
  • 1
  • 6
  • 10
  • why not just use HTML - `` – Martin Sep 15 '11 at 12:37
  • possible duplicate of [How to retain spaces in DropDownList - ASP.net MVC Razor views](http://stackoverflow.com/questions/5828697/how-to-retain-spaces-in-dropdownlist-asp-net-mvc-razor-views) – podiluska Sep 08 '15 at 15:02

3 Answers3

12

The easiest way to achieve this in C# is to use \xA0 instead of &nbsp; so you can avoid all that extra code.

Credit goes to this answer: How to retain spaces in DropDownList - ASP.net MVC Razor views

Community
  • 1
  • 1
BenC3
  • 716
  • 1
  • 8
  • 17
3

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 &nbsp;, 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("|", "&nbsp;"))

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("|", "&nbsp;")
Community
  • 1
  • 1
Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
0

It's because Razor always outputs entity-encoded html. You should be able to call (VS is updating on my machine right now, so I can't do a quick test) Html.Raw() in this context, like so:

var noiceList = new SelectList(new[] { new {ID = "", Name = ""}, 
                               new {ID = "y", Name = Html.Raw("Yes&nbsp;&nbsp;&nbsp;after3Space")}, 
             "ID", "Name");

EDIT: This does not work.

Greg Bair
  • 670
  • 6
  • 20
  • 1
    Unfortunately, the `SelectList` produces a list of `SelectListItem`s, which uses a regular old `string` for `Name` and `Value`. So, this won't work :( – Scott Rippey Dec 27 '11 at 23:03
  • Yeah, now that I think about it, it wouldn't work. Like I said, I couldn't test it because VS was updating at the time. – Greg Bair Jan 04 '12 at 17:50
  • I've been wondering if a future version of MVC will include support for nested HTML like this -- perhaps `SelectListItem.Name` could be a `HtmlString` instead? However, I don't think this is a "pretty" solution, because it should only be used for simple markup. – Scott Rippey Jan 05 '12 at 21:19