Lists and dictionaries
Editors for lists of text or numbers, and for keys and values
Editing a list
An array or list of strings or numbers, such as List<string> or int[], is rendered as a list editor. The list editor displays one row for each item, and a button that adds a row. To change the list editor's settings, call UseListEditor(). The example below allows at most eight highlights, and lets people reorder them:
fields.Add(trip => trip.Highlights).UseListEditor(list =>
{
list.MaxItems = 8;
list.Sortable = true;
list.Numbered = true;
list.MaxLength = 120;
list.AddText = "Add highlight";
});

The example below accepts only three-letter airport codes, changes them to upper case as people type, and rejects duplicates:
using StellarAdmin.UI;
fields.Add(trip => trip.DepartureAirports).UseListEditor(list =>
{
list.Unique = true;
list.Casing = ListEditorCasing.Upper;
list.Pattern = "^[A-Z]{3}$";
list.PatternMessage = "Use a three-letter airport code, such as LIS.";
});| Setting | Description |
|---|---|
MinItems, MaxItems | Sets the minimum or maximum number of items the list should contain. The defaults come from the property's [MinLength], [MaxLength] or [Length]. |
Sortable | Specifies whether items can be reordered. When enabled, users can reorder items by dragging them, or by pressing Alt+Up or Alt+Down. The default is false. |
Numbered | Specifies whether each row displays its row number. The default is false. |
Unique | Specifies whether each item must be unique. The default is false. |
Pattern, PatternMessage | Specifies a regular expression each item must match, and the error message when an item doesn't. |
MaxLength | Sets the maximum text length of an item. |
Min, Max, Step | Lists of numbers display a number editor. These properties set the minimum and maximum value of the numbers, as well as the step for the number editor. |
Casing | Changes the case of text as people type: ListEditorCasing.Upper or Lower. The default is None. |
Prefix, Suffix | Adds a prefix or suffix to display for each item, for example € or km. |
Placeholder | Sets the text displayed in an empty row. |
Align | Sets how each item is aligned in its row: ListEditorAlign.Start or End. By default, numbers align to the end, and text to the start. |
AddText, EmptyText | Sets the text of the button that adds a row, and the text displayed when the list is empty. The defaults are "Add item" and "No items yet". |
Blank rows aren't saved. EF Core maps a list of strings or numbers as a primitive collection, so the property needs no extra configuration.
The list editor edits text and numbers only. For a list of enum values, use a checkbox group or toggle group. For related records, use a multi-lookup.
Editing a dictionary
The dictionary editor allows you to edit C# dictionaries. It supports dictionaries where the key is either a string or enum, and the value is a string, number or enum. The dictionary editor displays one row for each entry, with the key beside the value.


To change the dictionary editor's settings, call UseDictionaryEditor(). In the callback, call Key() and Value() to configure the key and the value columns of the editor.
The example below labels the columns Cabin and Allowance, and accepts allowances up to 50 kg:
fields.Add(trip => trip.BaggageAllowance).UseDictionaryEditor(dictionary =>
{
dictionary.Key(key => key.Label = "Cabin");
dictionary.Value(value =>
{
value.Label = "Allowance";
value.Max = 50;
value.Suffix = "kg";
});
});The dictionary editor has the MinItems, MaxItems, Numbered, AddText and EmptyText settings of the list editor. The keys and the values have these settings:
| Setting | Description | Available for Key | Available for Value |
|---|---|---|---|
Label | Sets the column's label. | Yes | Yes |
Placeholder | Sets the text displayed in an empty key or value. | Yes | Yes |
Pattern, PatternMessage | Sets a regular expression the text must match, and the error message when it doesn't. | Yes | Yes |
MaxLength | Sets the maximum length of the text. | Yes | Yes |
Casing | Changes the case of text as users type: DictionaryEditorCasing.Upper or Lower. | Yes | Yes |
Min, Max, Step | For number values, sets the minimum and maximum value and the step for the number editor. | No | Yes |
Prefix, Suffix | Sets a prefix or suffix that must be displayed before or after each value, such as € or kg. | No | Yes |
Align | Sets how each value is aligned in its column: DictionaryEditorAlign.Start or End. | No | Yes |
DictionaryEditorCasing and DictionaryEditorAlign are in the StellarAdmin.UI namespace.
Selecting keys and values from a list
An enum key or value will display a select with the enum's members. You can specify a custom list options by calling UseItems() or UseResource() on the key or value, the same way as on a choice editor.
The example below selects the key from three languages, and the value from the guides:
using StellarAdmin.Resources.Editors;
fields.Add(trip => trip.GuidesByLanguage).UseDictionaryEditor(dictionary =>
{
dictionary.Key(key => key.UseItems([
new ChoiceItem("en", "English"),
new ChoiceItem("pt", "Portuguese"),
new ChoiceItem("ja", "Japanese"),
]));
dictionary.Value(value => value.UseResource<Guide>());
});Displaying validation errors
To display a validation error under a specific row, name the row in the error's member name, with the property and the row's index, which starts at 0. For a dictionary, add .Key or .Value to display the error under the key or the value, such as BaggageAllowance[1].Key.
The example below implements IValidatableObject, and displays an error under each highlight that only repeats the trip's name:
using System.ComponentModel.DataAnnotations;
public class Trip : IValidatableObject
{
public string Title { get; set; } = "";
public List<string> Highlights { get; set; } = [];
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
for (var index = 0; index < Highlights.Count; index++)
{
if (string.Equals(Highlights[index], Title, StringComparison.OrdinalIgnoreCase))
yield return new ValidationResult(
"A highlight can't be the trip's name.",
[$"{nameof(Highlights)}[{index}]"]);
}
}
}
