Overriding pages

Replace a page of the admin panel with your own Razor view

StellarAdmin renders each page of the admin panel with a Razor view that's compiled into the StellarAdmin package. To override a page, add a view with the same path to your app. As with any Razor class library, ASP.NET Core uses your app's view instead of the package's.

Most of the time, you'll keep StellarAdmin's content and add your own around it, such as a summary above the grid of an index page.

Adding the view imports

StellarAdmin's own _ViewImports.cshtml is compiled into the package, so it doesn't apply to your views. Before you override a page or a template, add a _ViewImports.cshtml to your app's Areas/StellarAdmin/Views/ folder that imports StellarAdmin's namespaces and Tag Helpers:

Areas/StellarAdmin/Views/_ViewImports.cshtml
@using StellarAdmin
@using StellarAdmin.UI
@using StellarAdmin.Areas.StellarAdmin
@using StellarAdmin.Areas.StellarAdmin.ViewModels
@using Voyager.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, StellarAdmin.UI
@addTagHelper *, StellarAdmin

Replace Voyager.Models with the namespace of your own models. If the folder has no _ViewImports.cshtml, Razor doesn't process tags such as <sa-index-page>, and writes them to the page as plain HTML. The page then looks empty where the tag is, without an error.

Overriding a resource's page

To override a page of one resource, add a view named after the page, in a folder named after the resource's slug. The view's model depends on the page:

PageViewModel
IndexAreas/StellarAdmin/Views/{slug}/Index.cshtmlResourceIndexPageViewModel<TResource>
CreateAreas/StellarAdmin/Views/{slug}/Create.cshtmlResourceFormPageViewModel
EditAreas/StellarAdmin/Views/{slug}/Edit.cshtmlResourceFormPageViewModel
ViewAreas/StellarAdmin/Views/{slug}/View.cshtmlResourceViewPageViewModel

For example, the index page of a resource with the bookings slug is Areas/StellarAdmin/Views/bookings/Index.cshtml. The folder name ignores case, so Views/Bookings/ works too.

To override a page for all resources, add ResourceIndex.cshtml, ResourceCreate.cshtml, ResourceEdit.cshtml or ResourceView.cshtml to Areas/StellarAdmin/Views/Shared/. The model of ResourceIndex.cshtml is IResourceIndexPageViewModel, because the view is used for resources of different types. A view in a resource's folder overrides the shared view for that resource.

Adding content to a page

Your view renders StellarAdmin's content of the page with the page's Tag Helper, such as <sa-index-page>, and adds its own content around it.

The example below displays the number of bookings that are waiting for confirmation above the grid of the bookings index page:

Areas/StellarAdmin/Views/bookings/Index.cshtml
@model ResourceIndexPageViewModel<Booking>
@using Microsoft.EntityFrameworkCore
@inject ApplicationDbContext Db
@{
    ViewData["Title"] = Model.Title;
    var pending = await Db.Bookings.CountAsync(booking => booking.Status == BookingStatus.Pending);
}

<sa-page-container>
    <sa-alert>
        <sa-alert-title>@pending bookings are waiting for confirmation</sa-alert-title>
    </sa-alert>
</sa-page-container>

<sa-index-page model="Model" />

Set ViewData["Title"] to Model.Title, as StellarAdmin's own views do, so the browser tab displays the page's title. Model.Items contains only the records on the current page of the grid, so count totals with your own query, as in the example above.

Search, scopes, sorting and paging still work on an overridden index page. They replace only the grid, and leave your content as it is.

To add content inside the page, such as a button next to Create, use the slots of the page's Tag Helper.

Adding a Delete button to the edit page

The edit page has no Delete button. To add one, override the edit page and add the button and its confirmation dialog to your view. Model.Delete is null when deleting isn't enabled:

Areas/StellarAdmin/Views/bookings/Edit.cshtml
@model ResourceFormPageViewModel
@{
    ViewData["Title"] = Model.Title;
}

<sa-form-page model="Model">
    @if (Model.Delete is { } delete)
    {
        <sa-slot-content name="form-leading-actions">
            <sa-button type="button" variant="ButtonVariant.OutlineDestructive"
                       command="show-modal" commandfor="delete-confirm-dialog">@delete.Title</sa-button>
        </sa-slot-content>
    }
</sa-form-page>

@if (Model.Delete is not null)
{
    <partial name="_FormDeleteDialog" model="Model.Delete" />
}

The view page's Delete button is part of <sa-view-page>, so an overridden view page keeps it.

Overriding other parts of the admin panel

You can also override these views:

On this page