Selecting several related records

Select the records of a many-to-many relationship, with a multi-lookup or a choice editor

StellarAdmin lets people select several related records on a form, such as a trip's tags.

Using many-to-many navigations on EF Core resources

A many-to-many navigation, such as Trip.Tags, holds the related records of a many-to-many relationship. On an EF Core resource, when you add the navigation property to a form, StellarAdmin will display a multi-lookup editor, which allows users to select several related records from the referenced resource. StellarAdmin finds the related resource from the many-to-many relationship in your EF Core model.

The example below registers tags as a resource, and adds Tags to the trip edit page:

public class Trip
{
    public int Id { get; set; }
    public string Title { get; set; } = "";
    public List<TripTag> Tags { get; set; } = [];
}
admin.AddEfCoreResource<TripTag>(resource => { /* ... */ });

admin.AddEfCoreResource<Trip>(resource =>
{
    resource.AllowEdit(edit => edit.Fields(fields =>
    {
        fields.Add(trip => trip.Title);
        fields.Add(trip => trip.Tags);
    }));
});

The entity the navigation refers to, such as TripTag, must be registered as a resource.

The field displays the selected tags as chips, and its Add tags button opens a sheet that searches the tags. When people save the form, StellarAdmin adds and removes the rows of the join table. The navigation doesn't need a setter, as long as it's initialized, such as with = [].

The trip edit page with the trip's tags as chips, and an Add tags button

Using a choice editor instead of a multi-lookup

A multi-lookup suits a long list. For a short list, such as tags, a checkbox group or a toggle group lets people select the records without opening a sheet. StellarAdmin lists the tags from the TripTag resource, as it does for a multi-lookup.

fields.Add(trip => trip.Tags).UseCheckboxGroupEditor();
The trip's tags as a checkbox group, with the trip's tags checked

A many-to-many navigation works with only these editors:

EditorMethod
Multi-lookupUseMultiLookupSheetEditor()
Checkbox groupUseCheckboxGroupEditor()
Toggle groupUseToggleGroupEditor()

If you specify an editor that does not support many-to-many navigations, such as a select, StellarAdmin leaves the field off the form.

Configuring the multi-lookup

To configure the multi-lookup, call UseMultiLookupSheetEditor(). Call Field() to configure the field that is displayed on the form, and Sheet() to configure the sheet that opens to the side, allowing users to select records.

The example below displays each selected tag in its own row, and sets the sheet's title:

using StellarAdmin.Resources.Editors;

fields.Add(trip => trip.Tags).UseMultiLookupSheetEditor(lookup =>
{
    lookup.Field(field => field.Layout = MultiLookupSheetEditorLayout.List);
    lookup.Sheet(sheet => sheet.Title = "Choose tags");
});
The trip's tags in a row each, and the sheet titled Choose tags

The Field() builder callback has the following members:

MemberDescription
LayoutConfigures how the selected records are displayed: MultiLookupSheetEditorLayout.Chips displays a chip for each record, List a row for each record with its media, title and description, and Summary a single row that names the first records and counts the others. The default is Chips.
EmptyTextSets the text of the button while nothing is selected. The default is "Add tags", named after the field's label.
ShowMediaSets whether the media of the selected records is displayed. The default is true.

The Sheet() builder callback has the following members:

MemberDescription
TitleSets the sheet's title. The default is the field's label.
SearchPlaceholderSets the hint text in the search input.
MinimumSearchLengthSpecifies the number of characters people must type before the sheet searches. The default is 0, which lists the records when the sheet opens.
PageSizeSets the number of records loaded at a time. The sheet displays a Load more button to load more records. The default is 20.
ShowMediaSets whether each record's media is displayed. The default is true.

To let people create a record from the multi-lookup, call EnableCreate(). This adds a New button at the bottom of the sheet, which opens the create page of the related resource, as described in creating a record from a lookup.

Limiting the number of records

Add [MinLength] and [MaxLength] to the navigation property to require a minimum or maximum number of related records to be selected.

[MinLength(1), MaxLength(3)]
public List<TripTag> Tags { get; set; } = [];

Relationships StellarAdmin can't edit

StellarAdmin allows editing a many-to-many navigation that goes straight to the related entity, such as Trip.Tags, when the related entity has a single-property primary key. This includes a navigation that EF Core maps through a join entity type with UsingEntity(), such as ASP.NET Core Identity's IdentityUserRole<string> behind a User.Roles navigation, as long as the join entity has only its two foreign keys. The following is not supported:

  • A navigation to a join entity, such as Trip.TripGuides, to a TripGuide entity with columns of its own.
  • A navigation to an entity with a composite primary key.
  • A many-to-many navigation in an inline row, because the grid has no editor that selects several records.

For a join entity with columns of its own or a composite key, use a list of keys on a form model, as described in selecting related records on a form model, and save the join rows yourself.

On a form model that wraps the entity, a many-to-many navigation that the field reads through the entity, such as form.Trip.Tags, is rendered as a multi-lookup, just as it is on the entity itself.

A property of the form model itself, or of a form model that you save with a handler, isn't part of your EF Core model, so StellarAdmin doesn't know which resource it refers to. For these, call UseResource() with the related resource's type.

The example below adds a multi-lookup for the form model's TagIds property, a list of the keys of the selected tags:

fields.Add(form => form.TagIds).UseMultiLookupSheetEditor(lookup =>
{
    lookup.UseResource<TripTag>();
});

To display the related records in the grid or on the view page, use BadgeDisplay. See displaying related records as badges.

On this page