I am having trouble creating indented items in a Razor MultiSelectBox.
It works perfectly when I manually write the HTML:
<select name="testfoo123" multiple="multiple" size="15">
<option value="PARENT1">Parent</option>
<option value="CHILD1"> I am indented</option>
<option value="CHILD2"> I am indented</option>
<option value="PARENT1">Parent2</option>
<option value="CHILD1"> I am indented</option>
<option value="CHILD2"> I am indented</option>
</select>
Razor’s HTML helpers, however, literally display the preceeding non-breaking space in the form. As expected, literal space ‘ ‘ characters for indentation are completely ignored.
The code I am using to generate the multi-select box follows:
@Html.ListBoxFor(model => mySelectedValues, new MultiSelectList(myValues), new { size = "15" })
I found a solution. First: A multi-select box is probably not the best choices for a tree-like control, but I’ll leave that decision up to the programmer.
Solution: Add a literal non-breaking space character. These are not filtered, but the HTML equivalent (as well as a normal space character) is.
Prepend each child with this character. Using a separate list as an example: