1

I'm using the Html.DropDownListFor() to display a dropdown with data from a list. The list items have a boolean property called IsPublic, defining whether this item is public or not.

Here's how I define the dropdown:

@Html.DropDownListFor(m => m.SelectedLayout,
                    new SelectList(Model.Layout, "LayoutString", "LayoutName", "IsPublic", 1))

As you can see, I'm grouping by the property IsPublic. As expected, the grouping is done the following way, with the following naming of the groups:

enter image description here

Notice how the grouping is done by the boolean value (true and false).

Is there a way for me to display this grouping as a custom string, though still grouping on the boolean value? So if IsPublic == true display the grouping name as Public and vice versa IsPublic == false display grouping name as Private?

labilbe
  • 3,501
  • 2
  • 29
  • 34
Detilium
  • 2,868
  • 9
  • 30
  • 65
  • You can do this by creating a new `SelectListGroup` and setting its `Name` property, (based on the `bool` value) and then creating new `SelectListItem` and setting the `Group` property as described in the second part of [this answer](http://stackoverflow.com/questions/37151572/constructing-a-select-list-with-optgroup-groups/37152663#37152663). You have not shown the model or what values it migh contain so bit hard to give a code example –  May 30 '16 at 07:46

1 Answers1

2

You could simply use a get only property in your class, and use it for grouping.

public string DisplayPublic {
    get { return IsPublic ? "Public" : "Private"; }
}
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122