Module Pages
A module page is a page whose body is a single container element and whose client side is an ES module: the page view only
renders the markup and the <script type="module"> tag, and the module's default export (pageInit) creates the widget that
fills the container. Serenity's generated pages (*Page.cs + *Page.ts) are module pages, and a feature can add one with a
few lines.
The helpers live in Serenity.Extensions (namespace Serenity.Web), in
ModulePageExtensions.
Creating a Module Page
A controller action returns GridPage, PanelPage or ModulePage:
[PageAuthorize(Northwind.PermissionKeys.General)]
public class OrderPageController : Controller
{
[Route("Northwind/Order")]
public ActionResult Index() => this.GridPage<OrderRow>("Northwind/Order/OrderGrid");
}
GridPagerenders<div id="GridDiv"></div>as the body, for a grid that fills the page.PanelPagerenders<div id="PanelDiv"></div>, for a page that hosts a panel (e.g. a master-detail layout) instead.ModulePagerenders no body of its own; give it aModulePageModelwithHtmlMarkupwhen the page needs a different container.
The module can also be given as a path relative to ~/esm/Modules, e.g. "@/Northwind/Order/OrderGrid"; the helper expands
it and appends .js when it is missing.
ModulePageModel
ModulePageModel has:
| Member | Meaning |
|---|---|
Module |
The module name or path (required) |
PageTitle |
A LocalText used as the page title, also the browser title |
Options |
Options passed to the module's pageInit, e.g. a starting filter |
Layout |
The layout to render the page with |
HtmlMarkup |
The body markup, when the default container is not what the page needs |
PageId |
The id of the page element |
The result, ModulePageResult, has fluent PageTitle
and Layout helpers, and PageTitle<TRow>() takes the title from the row's EntityPlural text:
return this.ModulePage(new ModulePageModel
{
Module = "Northwind/Order/OrderDetail",
Options = new { OrderId = id }
})
.PageTitle<OrderRow>()
.Layout(MVC.Views.Shared._LayoutNoNavigation);
The row overloads of GridPage do the same thing for the title, e.g. this.GridPage<OrderRow>("Northwind/Order/OrderGrid")
uses Db.Northwind.Order.EntityPlural.
Module Page Layouts
The layout of a module page is, in order, the Layout of the model, the ModulePageLayout attribute of the controller, or the
default layout of the application. The attribute is useful when every page of a controller uses the same layout, which is the
only way for a page that is not based on a view file (e.g. a panel page created from a module script only) to tell its
layout:
[ModulePageLayout("~/Views/Shared/_LayoutNoNavigation.cshtml")]
public class ReportsPageController : Controller
{
[Route("Reports")]
public ActionResult Index() => this.GridPage<ReportRow>("Reports/ReportGrid");
}
ModulePageLayoutAttribute is inherited, so the
controllers derived from a controller that has it use the same layout, and a layout the page sets itself still wins. A project
can derive its own attribute that passes the layout, e.g. [BasicSamplesLayout], and put it on the controllers of a module.
How the Page Is Rendered
The ModulePage.cshtml view of Serenity.Extensions renders the model: it sets the page title, page id and layout, includes
the CSS file next to the module (Html.AutoIncludeModuleCss), renders HtmlMarkup, and emits the <script type="module">
that imports the module and calls its default export with Options. The module page needs no view of its own, which is why a
feature library can ship pages without the Razor SDK.
A view of your own can use the ModulePageInit helper to render the same script tag for a module.
The page's permission is the one of the controller or action ([PageAuthorize]), as for any other action. A module that the
browser should not be able to load for a user without the permission can be protected with the ESM access control, see
ESM Access Control.
See Also
- Navigation — adding the page to the menu
- Script & CSS Bundling —
ModulePageInitand the other layout helpers - Static Assets — how the ES modules of the features are served
- ModulePageExtensions (API reference)