Skip to content
Michal Altair Valášek edited this page May 28, 2024 · 3 revisions

Attribute

[Select]
[Select(ListPropertyName = "Name")]

Template result

<select>
    <option value="...">...</option>
    <option value="...">...</option>
    ...
    <option value="...">...</option>
</select>

Notes

This attribute expects additional property of type IEnumerable<SelectListItem>, containing the options. Unless specified otherwise with ListPropertyName, the list property name should be name of the original property with List suffix added, ie. CategoryList for Category.

[Select]
public int Category { get; set; }

// This property is used to populate the select list and is not scaffolded
[ScaffoldColumn(false)]
public IEnumerable<SelectListItem> CategoryList {
    get {
        var group1 = new SelectListGroup() { Name = "Group 1" };
        var group2 = new SelectListGroup() { Name = "Group 2" };
        return [
            new () { Value = "1", Text = "Category One" },
            new () { Value = "2", Text = "Category Two" },
            new () { Value = "3", Text = "Category Three" },
            new () { Value = "4", Text = "Category 1.1", Group = group1 },
            new () { Value = "5", Text = "Category 1.2", Group = group1 },
            new () { Value = "6", Text = "Category 1.3", Group = group1 },
            new () { Value = "7", Text = "Category 2.1", Group = group2 },
            new () { Value = "8", Text = "Category 2.2", Group = group2 },
            new () { Value = "9", Text = "Category 2.3", Group = group2 }
        ];
    }
}

Clone this wiki locally