Adding widgets
Add stats, lists and other blocks to the home page, index pages, view pages and forms
Introduction
Widgets are small panels you can add to StellarAdmin that typically displays data as a chart, table, or counter. Widgets can be added to the admin panel's home page, and to a resource's index page, view page, and create and edit pages.


Adding a widget
To add widgets to a page, call Widgets() in the page's builder and add widgets using the widget builder's Add() method. Add() takes a name that identifies the widget on the page, and the name of the partial view that renders the widget contents.
The example below adds a widget to the home page that renders the _QuickLinksWidget partial view:
using StellarAdmin.Widgets;
builder.Services.AddStellarAdmin(admin =>
{
admin.ConfigureHomePage(home => home.Widgets(widgets =>
{
widgets.Add("Quick links", "_QuickLinksWidget");
}));
});StellarAdmin renders the partial view with the page, and passes the page's widget context as its model. The _QuickLinksWidget partial view below links to the bookings create page:
@model WidgetContext
<sa-card>
<sa-card-header>
<sa-card-title>Quick links</sa-card-title>
</sa-card-header>
<sa-card-content>
<sa-link-button variant="ButtonVariant.Outline" asp-area="StellarAdmin"
asp-controller="bookings" asp-action="Create">New booking</sa-link-button>
</sa-card-content>
</sa-card>Put the partial view of a home page widget in Areas/StellarAdmin/Views/Home/. Put the partial view of a resource's widget in Areas/StellarAdmin/Views/{resourceSlug}/, for example Areas/StellarAdmin/Views/bookings/. To use a partial view on several pages, put it in Areas/StellarAdmin/Views/Shared/.
Most widgets display data, such as a count from your database. To load the data, you write a handler, and pass it as the type argument of Add(), such as widgets.Add<BookingStatsHandler>("Bookings", "_BookingStatsWidget"). StellarAdmin loads a widget with a handler after the page has rendered, and passes the data to the partial view as its model. Some examples on this page use handlers, which are described in loading widget data.
Using the widget context
The widget context contains information about the request and the page the widget is displayed on, such as the signed-in user or the record a view page displays. You receive the widget context in the following places:
- As the model of the partial view, when the widget has no handler.
- As the parameter of the delegates you pass to
ShowWhen()andParameters().
The example below adds a notes widget to the bookings view page, and uses the widget context in ShowWhen() to hide the widget for cancelled bookings:
builder.Services.AddStellarAdmin(admin =>
{
admin.AddEfCoreResource<Booking>(resource =>
{
resource.AllowView(view =>
{
view.Widgets(widgets =>
{
widgets.Add("Notes", "_BookingNotesWidget", widget =>
{
widget.ShowWhen(context => context.Record.Status != BookingStatus.Cancelled);
});
});
});
});
});The _BookingNotesWidget partial view reads the booking from the Record property of the context and displays its special requests:
@model RecordWidgetContext<Booking>
<sa-card>
<sa-card-header>
<sa-card-title>Notes</sa-card-title>
</sa-card-header>
<sa-card-content>
<p>@(Model.Record.SpecialRequests ?? "No special requests")</p>
</sa-card-content>
</sa-card>Every widget context has the request's HttpContext and User. You can also resolves services from DI via the Services property. The type of the widget context depends on the page and may have additional properties as described below.
| Page | Widget context | Additional properties |
|---|---|---|
| Home page | WidgetContext | — |
| Index page | IndexWidgetContext<TResource>, where TResource is the resource's type | Search, Scope |
| View and edit pages | RecordWidgetContext<T>, where T is the form's model | Key, Record |
| Create page | CreateWidgetContext<T>, where T is the form's model | — |
On the view and edit pages, Record is the record the page displays or the form edits, and T is its type. Usually T is the resource's type, such as Booking. If the view page or the form has its own model, T is the model's type, and Record is an instance of that model.
The additional properties listed above contain the following values:
| Property | Description |
|---|---|
Search | The text the grid is searched for, trimmed, or null when the grid isn't searched. |
Scope | The id of the selected scope, or null when the resource has no scopes. |
Key | The key of the record the page displays or the form edits, as it appears in the page's URL. |
Record | The record the page displays or the form edits. When the edit form is displayed again because it isn't valid, the record has the values that were submitted. |
StellarAdmin renders the widgets of an index page again when the search or the scope changes, so Search and Scope have the new values. Sorting and paging the grid leave the widgets as they are.
Adding widgets to each page
Home page
To add widgets to the home page, call Widgets() in ConfigureHomePage(). The example below adds a bookings widget and a quick links widget:
builder.Services.AddStellarAdmin(admin =>
{
admin.ConfigureHomePage(home =>
{
home.Widgets(widgets =>
{
widgets.Add<BookingStatsHandler>("Bookings", "_BookingStatsWidget");
widgets.Add("Quick links", "_QuickLinksWidget");
});
});
});Index page
To add widgets to an index page, call Widgets() in Index(). The example below adds a booking totals widget above the bookings grid, and an upcoming departures widget beside it:
builder.Services.AddStellarAdmin(admin =>
{
admin.AddEfCoreResource<Booking>(resource =>
{
resource.Index(index =>
{
index.Widgets(widgets =>
{
widgets.Add<BookingTotalsHandler>("Totals", "_BookingTotalsWidget", widget =>
{
widget.Parameters(context => new BookingTotalsParameters(context.Scope, context.Search));
});
widgets.Add<UpcomingDeparturesHandler>("Upcoming departures", "_UpcomingDeparturesWidget", widget =>
{
widget.Placement = WidgetPlacement.Aside;
});
});
});
});
});

View page
To add widgets to a view page, call Widgets() in AllowView(). The example below adds an other bookings widget beside a booking's fields, and a notes widget below them:
builder.Services.AddStellarAdmin(admin =>
{
admin.AddEfCoreResource<Booking>(resource =>
{
resource.AllowView(view =>
{
view.Widgets(widgets =>
{
widgets.Add<OtherBookingsHandler>("Other bookings", "_OtherBookingsWidget", widget =>
{
widget.Placement = WidgetPlacement.Aside;
widget.Parameters(context => new OtherBookingsParameters(context.Record.Id));
});
widgets.Add("Notes", "_BookingNotesWidget", widget =>
{
widget.Placement = WidgetPlacement.Bottom;
});
});
});
});
});

Create and edit pages
To add widgets to the create and edit pages, call Widgets() in AllowCreate() and AllowEdit(). The example below adds a tip widget to the trips create page, and an upcoming departures widget beside the trips edit form:
builder.Services.AddStellarAdmin(admin =>
{
admin.AddEfCoreResource<Trip>(resource =>
{
resource.AllowCreate(create =>
{
create.Widgets(widgets => widgets.Add("Tip", "_TripTipWidget"));
});
resource.AllowEdit(edit =>
{
edit.Widgets(widgets =>
{
widgets.Add<UpcomingDeparturesHandler>("Upcoming departures", "_UpcomingDeparturesWidget", widget =>
{
widget.Placement = WidgetPlacement.Aside;
widget.Parameters(context => new UpcomingDeparturesParameters(context.Record.Id));
});
});
});
});
});

Specifying the widget position
On a resource's index, view, create and edit pages, set Placement in the widget's callback to specify where the widget is displayed. Placement can be one of the following:
| Value | Description |
|---|---|
WidgetPlacement.Top | Above the grid, the fields or the form. This is the default. |
WidgetPlacement.Aside | In a column beside the grid, the fields or the form. On the create and edit pages, the column stays in view as the form scrolls. When the page is narrow, the column is displayed below the grid, the fields or the form. |
WidgetPlacement.Bottom | Below the grid, the fields or the form. |
The example below displays a list of upcoming departures beside the bookings grid:
builder.Services.AddStellarAdmin(admin =>
{
admin.AddEfCoreResource<Booking>(resource =>
{
resource.Index(index =>
{
index.Widgets(widgets =>
{
widgets.Add<UpcomingDeparturesHandler>("Upcoming departures", "_UpcomingDeparturesWidget", widget =>
{
widget.Placement = WidgetPlacement.Aside;
});
});
});
});
});The home page displays its widgets in a grid, and has no other placements. If you set Placement on a home page widget, StellarAdmin displays the widget in the grid, and reports a configuration problem.
The widgets on the home page, and the at the tip and bottom of the other pages, are displayed in a grid of 12 columns. Configure how many columns a widget spans by setting the Span property in the widget builder to a number between 1 and 12. Span defaults to 12.
In a narrow window, every widget spans the whole row, and a widget with a span of 6 or less spans half of it at medium widths. Widgets in the same row are stretched to the same height, so their cards line up.
The example below makes the quick links widget on the home page span half of the grid:
builder.Services.AddStellarAdmin(admin =>
{
admin.ConfigureHomePage(home => home.Widgets(widgets =>
{
widgets.Add("Quick links", "_QuickLinksWidget", widget =>
{
widget.Span = 6;
});
}));
});Widgets on the side of a page are stacked in one column, so Span doesn't apply to them.
Showing a widget to specific users
To display a widget only to certain users, call RequireAuthorization(). StellarAdmin doesn't display the widget to users who don't meet the requirements. The example below displays the quick links widget only to users in the Agent role:
builder.Services.AddStellarAdmin(admin =>
{
admin.ConfigureHomePage(home => home.Widgets(widgets =>
{
widgets.Add("Quick links", "_QuickLinksWidget", widget =>
{
widget.RequireAuthorization(policy => policy.RequireRole("Agent"));
});
}));
});