liveBook · Manning

SelectListGroup

As explained in ASP.NET Core in Action, 3e

SelectListGroup is an ASP.NET Core MVC data structure representing a group of related options in a select list.

SelectListGroup represents a named group of related options in an ASP.NET Core MVC select list. Assign a group to each SelectListItem through its Group property, and the Select Tag Helper renders grouped options as HTML optgroup elements. section 18.2.4 section 18.6

Group language options

var dynamic = new SelectListGroup { Name = "Dynamic" }; var @static = new SelectListGroup { Name = "Static" }; Items = new List<SelectListItem> { new SelectListItem { Value = "js", Text = "JavaScript", Group = dynamic }, new SelectListItem { Value = "python", Text = "Python", Group = dynamic }, new SelectListItem { Value = "cpp", Text = "C++", Group = @static } }; <select asp-for="SelectedValues" asp-items="Model.Items"></select> The rendered select contains Dynamic and Static optgroup elements. Items without a group remain outside the groups. section 18.2.4

Listing 18.7 Adding Groups to SelectListItems to create optgroup elements
public class SelectListsModel: PageModel
{
    [BindProperty]
    public IEnumerable<string> SelectedValues { get; set; }        #1
    public IEnumerable<SelectListItem> Items { get; set; }
 
    public SelectListsModel()                                      #2
    {
        var dynamic = new SelectListGroup { Name = "Dynamic" };    #3
        var @static = new SelectListGroup { Name = "Static" };     #3
        Items = new List<SelectListItem>
        {
            new SelectListItem {
                Value= "js",
                Text="Javascript",
                Group = dynamic                                    #4
            },
            new SelectListItem {
                Value= "cpp",
                Text="C++",
Group = @static                                    #4
            },
            new SelectListItem {
                Value= "python",
                Text="Python",
Group = dynamic                                    #4
            },
            new SelectListItem {                                   #5
                Value= "csharp",                                   #5
                Text="C#",                                         #5
            }
        };
    }
}
#1 Holds the selected values where multiple selections are allowed
#2 Initializes the list items in the constructor
#3 Creates a single instance of each group to pass to SelectListItems
#4 Sets the appropriate group for each SelectListItem
#5 If a SelectListItem doesn’t have a Group, it won’t be added to an &lt;optgroup&gt;.

View the complete grouped SelectListItem example, including dynamic and static groups. section 18.2.4

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains group creation, item assignment, and generated optgroup elements, but does not descend into framework internals.
Examples
Uses Dynamic and Static language groups containing JavaScript, Python, and C++ options.
What it assumes
Assumes basic Razor and model binding while establishing SelectListItem collections and select-list rendering.
What you can do after
Apply it via the Select Tag Helper to render grouped single-select or multiselect controls.
How it connects
Connects SelectListGroup with SelectListItem, the Select Tag Helper, select lists, and HTML optgroup elements.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.