Custom displays
Write your own display type, with settings and its own template
Create a custom display
When none of the built-in display types suit your needs, you can write your own. A display template is simpler, but it applies to the property everywhere and has no settings. Use a custom display instead when you want to be more selective with which columns and fields use it using UseDisplay(), or if you need to add additional settings for configuration on a per column or field level.
A custom display has two parts: a class with the display type's settings, and a Razor template that renders the value. The class derives from FieldDisplay, and names its template in TemplateName.
The example below displays a booking's status as a badge, with a setting that indicates whether an icon must be displayed:
using StellarAdmin.Resources.Displays;
public class BookingStatusDisplay : FieldDisplay
{
public override string TemplateName => "BookingStatus";
public bool ShowIcon { get; set; }
}The template inherits FieldDisplayView<TDisplay>, and derives the badge's variant from the value:
@using StellarAdmin.Areas.StellarAdmin
@using StellarAdmin.UI
@inherits FieldDisplayView<BookingStatusDisplay>
@{
var variant = Model switch
{
BookingStatus.Confirmed => BadgeVariant.Default,
BookingStatus.Cancelled => BadgeVariant.Destructive,
_ => BadgeVariant.Secondary,
};
}
<sa-badge variant="@variant">
@if (Display.ShowIcon)
{
<sa-icon name="circle-check" />
}
@FormatValue()
</sa-badge>To use the display type, call UseDisplay() with its type:
columns.Add(booking => booking.Status, column =>
column.UseDisplay<BookingStatusDisplay>(status => status.ShowIcon = true));Working with templates
The grid template must be placed Areas/StellarAdmin/Views/Shared/GridDisplayTemplates/, and the template for display fields and view pages in Areas/StellarAdmin/Views/Shared/DisplayTemplates/, using the filename returned from TemplateName. If both templates are the same, you still need to create separate ones in each location.
Since your templates inherit from FieldDisplayView<BookingStatusDisplay>, they have the following properties:
| Property | Description |
|---|---|
Model | The value that must be rendered, as an object?. It is up to you to cast it to the correct type. |
Display | The display type, with your settings. |
DisplayData | The data the display type's handler loaded. See loading data for the display. |
To use consistent string formatting as StellarAdmin's own display types, call FormatValue(). It uses the property's [DisplayFormat], an enum member's [Display(Name)], and the title of a record of a registered resource. To use a .NET format string instead, pass it to FormatValue(), such as FormatValue("N2").
Displaying empty values
By default, StellarAdmin doesn't call the template for a null or empty value, and displays the empty text instead. For some display type, however, you may want to handle rendering the empty state yourself - such as a placeholder for a missing photo. To do this, you can override RendersEmptyValue and return true.
public override bool RendersEmptyValue => true;Loading data for the display
In the previous examples, the display only used the data from the model for rendering. However, sometimes your display may need to load data from and external service. To do this you need to create a handler which derives from FieldDisplayHandler<TDisplay>, and returns the data from PrepareAsync(). Its constructor must take the display type as parameter, as well as any other services the handler needs.
The example below displays an airport's name for a stored airport code, and loads the name from an IAirportDirectory service:
using StellarAdmin.Resources.Displays;
public class AirportDisplayHandler(AirportDisplay display, IAirportDirectory airports)
: FieldDisplayHandler<AirportDisplay>(display)
{
public override async Task<object?> PrepareAsync(
FieldDisplayContext context,
CancellationToken cancellationToken)
{
return context.Value is string code
? await airports.FindNameAsync(code, cancellationToken)
: null;
}
}Connect the handler to the display type by implementing IFieldDisplay<THandler> on the display type:
public class AirportDisplay : FieldDisplay, IFieldDisplay<AirportDisplayHandler>
{
public override string TemplateName => "Airport";
}A display type with a handler only works in display fields and on view pages, but not in the grid. If a column uses one, the column uses its default display type instead.
The template can read the data from the DisplayData property, which is of type object?, so cast it to the type the handler returns:
@using StellarAdmin.Areas.StellarAdmin
@inherits FieldDisplayView<AirportDisplay>
@(DisplayData as string ?? FormatValue()) (@Model)

FieldDisplayContext holds the field's name, the record, and the field's value, for a handler that loads data based on them.
| Member | Description |
|---|---|
FieldName | The property path of the field. For a field that computes its value, it's the field's title in PascalCase, such as TotalPrice for "Total price". |
Model | The record the field belongs to. |
Value | The value of the field. |