List Editor
An editor for a list of text or number values, with a row per item that can be added, removed and reordered
<sa-list-editor asp-for="Highlights" sortable="true" max-length="120"
placeholder="e.g. Tasting at a family-run tasca" add-text="Add highlight"
empty-text="No highlights yet" />Usage
<sa-list-editor> edits an array or list of strings or numbers, such as a List<string> or an int[]. Bind it with asp-for. Each item is a row in one box, and Add is the last row. A list of enum members is better as a checkbox group or toggle group, and a list of related records needs a lookup, not this editor.
<sa-list-editor asp-for="..." />Each row posts an indexed name, such as Highlights[0] and Highlights[1], which MVC binds to the collection. A hidden marker posts with the rows, so removing every item binds an empty list instead of keeping the old one. Rows left blank don't post.
The rows are rendered on the server, so the values show before the script runs. Adding, removing and reordering rows needs stellar-admin.js.
Enter in the last row adds a row after it, and Backspace or Escape in a blank row removes it. Escape in a row with a value is left to the page, so it still closes a dialog the editor is in. ↑ and ↓ move between rows. Pasting several lines into a row adds a row for each line.
Examples
Numbered
Set numbered to show each item's number in a gutter. Set sortable to let the items be reordered by dragging their handle, or with Alt+↑ and Alt+↓. With both, the number turns into the handle when the row is hovered or focused.
<sa-list-editor asp-for="Highlights" sortable="true" numbered="true" add-text="Add highlight" />Numbers
A list of numbers renders number inputs, aligned to the end so that their digits line up. min and max set each item's range, and step its step, which is 1 for a whole-number type and any step otherwise. align sets the alignment of the items, numbers or text.
<sa-list-editor asp-for="DepartureDays" min="1" max="31" unique="true" placeholder="1–31"
add-text="Add day" />Item checks
pattern is a regular expression each text item must match, and pattern-message the message when it doesn't. max-length limits an item's length, and unique rejects an item that is already in the list, ignoring case. casing changes an item to uppercase or lowercase as it is typed.
<sa-list-editor asp-for="ArrivalAirports" unique="true" casing="ListEditorCasing.Upper"
pattern="[A-Z]{3}" pattern-message="Use a three-letter IATA code, like LIS."
placeholder="LIS" add-text="Add airport" />The editor checks a row when focus leaves it, again as the row is changed once it shows an error, and every row when the form is submitted. A row's error shows under the row, and the submit is blocked until the errors are fixed. Without pattern-message, the browser's own messages are shown. The browser checks can be bypassed, so check the items again on the server.
Prefix and suffix
prefix and suffix show text before and after each item, such as a currency symbol or a unit.
<sa-list-editor asp-for="DailyDistances" min="0" step="0.5" suffix="km" numbered="true"
add-text="Add stage" />Empty
empty-text replaces the "No items yet" message of an empty list, and add-text the text of the Add row.
<sa-list-editor asp-for="PackingList" add-text="Add something to pack"
empty-text="Nothing to pack yet. Travellers get the standard list." />Number of items
min-items and max-items set how many items the list can have. They default to the lengths of a [MinLength], [MaxLength] or [Length] attribute on the property. With a maximum, the Add row shows the count, such as "3 of 8", and is disabled when the list is full. Too few items blocks the submit and shows an error under the field.
Validation
An error in ModelState for an item, such as Highlights[1], shows under that row, and an error for the property shows under the field. The demo adds both to ModelState.
<sa-list-editor asp-for="Highlights" add-text="Add highlight" />Read only
Set readonly="true" to show the items in read-only inputs, without adding, removing or reordering. The items can still be focused, and a read-only list posts its values.
<sa-list-editor asp-for="Highlights" readonly="true" />Disabled
Set disabled="true" to fade the editor and disable its inputs and buttons. A disabled list can't be focused and posts no values. A disabled <fieldset> around the editor disables it too.
<sa-list-editor asp-for="Highlights" disabled="true" />Accessibility
The box is a group labeled by the field's label. Each input is named by its number, such as "Item 2", and the buttons as "Remove item 2" and "Move item 2". A focused handle moves its row with ↑ and ↓. Escape cancels a drag. A row's error is linked to its input with aria-describedby. Under prefers-reduced-motion, rows don't slide as they move.
Part classes
Pass a ListEditorClassNames instance from the StellarAdmin.UI namespace using class-names. Each property accepts an optional string of space-separated CSS classes, which are added to the part's existing classes.
| Property | Target |
|---|---|
Root | The generated field wrapper, when the control renders an implicit field. |
Label | The generated field label, when rendered. |
Description | The generated help text, when rendered. |
Error | The generated validation message element, when rendered. |
Content | The generated container around the label and supporting text in a horizontal implicit field. |
Control | The editor's box. |
Root, Label, Description, Error, and Content are inherited from FieldClassNames; see Field part classes.
API Reference
<sa-list-editor>
Renders a web component with a row per item, the Add row, and a hidden marker that posts with the rows.
Prop
Type
The editor throws when it has no asp-for, or is bound to something other than an array or list of strings or numbers.