Display templates
Display a property's value with your own Razor template
A display template is a Razor view that renders a property's value in place of its display type. To use a display template for a property, add [UIHint] with the template's name:
public class Booking
{
[UIHint("BookingStatus")]
public BookingStatus Status { get; set; }
}The property then needs two templates with that name: one for the grid, and one for display fields and view pages. The example below displays a canceled booking's status as a red badge:
@using StellarAdmin.UI
@model BookingStatus
<sa-badge variant="@(Model == BookingStatus.Cancelled ? BadgeVariant.Destructive : BadgeVariant.Secondary)">
@Model
</sa-badge>@using StellarAdmin.UI
@model BookingStatus
<sa-badge variant="@(Model == BookingStatus.Cancelled ? BadgeVariant.Destructive : BadgeVariant.Secondary)">
@Model
</sa-badge>



The template's model is the property value. StellarAdmin doesn't call the template for a null or empty value, and displays the empty text instead.
A column or display field that calls UseDisplay() uses that display type instead of the template.
Where to put the templates
| Template for | Folder |
|---|---|
| The grid | Areas/StellarAdmin/Views/Shared/GridDisplayTemplates/ |
| Display fields and view pages | Areas/StellarAdmin/Views/Shared/DisplayTemplates/ |
If a template is missing, the page that displays the property fails with an exception that lists the folders StellarAdmin searched.
The sa-* Tag Helpers need a _ViewImports.cshtml in Areas/StellarAdmin/Views/. Without it, the tags render as plain text. Add a @using for your model's namespace to the template, or to the _ViewImports.cshtml.
Changing a built-in display type
To change how a built-in display type renders everywhere, add a template with the built-in template's path. The path is the display type's name without Display, in a Displays folder. For BooleanDisplay, the paths are:
Areas/StellarAdmin/Views/Shared/GridDisplayTemplates/Displays/Boolean.cshtmlAreas/StellarAdmin/Views/Shared/DisplayTemplates/Displays/Boolean.cshtml
To read the display type's settings, the template inherits FieldDisplayView<TDisplay>. The example below displays a bool in the grid as a badge with the TrueText or FalseText:
@using StellarAdmin.Areas.StellarAdmin
@using StellarAdmin.Resources.Displays
@using StellarAdmin.UI
@inherits FieldDisplayView<BooleanDisplay>
<sa-badge variant="@(Model is true ? BadgeVariant.Default : BadgeVariant.Outline)">
@(Model is true ? Display.TrueText : Display.FalseText)
</sa-badge>

See custom displays for the properties the template has.
Choosing between a display template and a custom display
A [UIHint] applies to the property everywhere it's displayed, and the template has no settings. To use a template for some columns or fields only, or to give it settings, write a custom display instead.