Defining resources

Register the types you manage in the admin panel

Introduction

A resource is a type you manage in the admin panel, such as Trip or Booking. This is most typically an EF Core entity, though StellarAdmin is not limited to EF Core.

Every resource has an index page that lists its records inside a grid. You can also enable create, edit, and view pages for the resources, as well as allow deleting records.

Adding an EF Core resource

To manage an entity from your DbContext, call AddEfCoreResource<T>(). The example below adds a resource for the Trip entity with an index page with two columns displayed in the grid. It also defines an edit page, allowing you to edit those same two field from the grid.

using StellarAdmin.EntityFrameworkCore;

admin.AddEfCoreResource<Trip>(resource =>
{
    resource.Index(index => index.Columns(columns =>
    {
        columns.Add(trip => trip.Title);
        columns.Add(trip => trip.BasePrice);
    }));
    resource.AllowEdit(edit => edit.Fields(fields =>
    {
        fields.Add(trip => trip.Title);
        fields.Add(trip => trip.BasePrice);
    }));
});

StellarAdmin finds the registered DbContext that maps Trip, and reads the entity's key from the EF Core model. The key must be a single int, long, Guid or string property.

If more than one DbContext maps the entity, you must explicitly link the entity to the correct one by calling UseDbContext<TContext>().

resource.UseDbContext<ApplicationDbContext>();

Adding pages

The Index() method configures the resource's index page. It allows you to declare the columns displayed in the index grid, as well as sorting, paging, search, and scopes. All of these are discussed in more detail in the index pages section.

Besides the index page, you can also enable other pages for the resource by calling the methods below on your resource builder.

MethodAdds
AllowCreate()Enables the resource create page and will add a create button on the index page.
AllowEdit()Adds a resource edit page allowing you to edit a resource. An edit action will be displayed on each row of the index page.
AllowView()Adds a resource view page which provides a read-only view of a record. A view action will be displayed on each row in the index page.
AllowDelete()Enables deletion of records. A delete action will be displayed on each row and on the view page, as well as on the View page.

Each method takes a callback that configures the page or action.

Changing the URLs

A resource's Index page will be at the URL /stellaradmin/{resourceSlug}. This is also called the base url of the resource and all other pages are based on that. For example, the create page is located at /stellaradmin/{resourceSlug}/create, the edit page at /stellaradmin/{resourceSlug}/edit/{id}, etc.

So, in the case of a TripExtra resource, the index page is /stellaradmin/trip-extras. The other pages are at /stellaradmin/trip-extras/create, /stellaradmin/trip-extras/edit/{id} and /stellaradmin/trip-extras/view/{id}.

The resource slug is automatically derived from the plural form of the resource's type. In the URLs above, the resource TripExtra has a resource slug trip-extras. You can change the slug of a resource when you add the resource by passing the slug parameter. For example, you can change the slug of the TripExtra resource to "add-ons" as follows:

admin.AddEfCoreResource<TripExtra>("add-ons", resource =>
{
    // ...
});

Changing the labels

Page titles and action labels, are derived from the resource's singular and plural label. For example, the index page of the TripExtra resource has a default label of "Trip extras" - i.e. using the plural label. On the index page, the create button will have a label which uses the singular label of the resource, for example "Create trip extra".

You can override the singular and plural label when adding a resource.

admin.AddEfCoreResource<TripExtra>(resource =>
{
    resource.SingularLabel = "Add-on";
    resource.PluralLabel = "Add-ons";
});

You can also override individual pages titles and button labels when configuring the various pages such as the Index page and Edit page. You can also override the pattern used to generate these labels by setting the global labels.

On this page