4.8 KiB
title | author | ms.author | description | monikerRange | ms.date | uid |
---|---|---|---|---|---|---|
Using DisplayTemplates and EditorTemplates | Ducki | riande | How to use DisplayTemplates and EditorTemplates in ASP.NET Core. | >= aspnetcore-2.1 | 05/22/2022 | mvc/views/display-templates |
Display and Editor templates in ASP.NET Core
Display and Editor templates specify the user interface layout of custom types. Consider the following Address
model:
A project that scaffolds the Address
model displays the Address
in the following form:
A web site could use a Display Template to show the Address
in standard format:
Display and Editor templates can also reduce code duplication and maintenance costs. Consider a web site that displays the Address
model on 20 different pages. If the Address
model changes, the 20 pages will all need to be updated. If a Display Template is used for the Address
model, only the Display Template needs to be updated. For example, the Address
model might be updated to include the country or region.
Tag Helpers provide an alternative way that enables server-side code to participate in creating and rendering HTML elements in Razor files. For more information, see Tag Helpers compared to HTML Helpers.
Display templates
DisplayTemplates
customize the display of model fields or create a layer of abstraction between the model values and their display.
A DisplayTemplate
is a Razor file placed in theDisplayTemplates
folder:
- For Razor Pages apps, in the
Pages/Shared/DisplayTemplates
folder. - For MVC apps, in the
Views/Shared/DisplayTemplates
folder or theViews/ControllerName/DisplayTemplates
folder. Display templates in theViews/Shared/DisplayTemplates
are used by all controllers in the app. Display templates in theViews/ControllerName/DisplayTemplates
folder are resolved only by theControllerName
controller.
By convention, the DisplayTemplate
file is named after the type to be displayed. The Address.cshtml
template used in this sample:
The view engine automatically looks for a file in the DisplayTemplates
folder that matches the name of the type. If it doesn't find a matching template, it falls back to the built in templates.
The following code shows the Details view of the scaffolded project:
The following code shows the Details view using the Address Display Template:
To reference a template whose name doesn't match the type name, use the templateName
parameter in the xref:Microsoft.AspNetCore.Mvc.Rendering.HtmlHelperDisplayExtensions.DisplayFor%2A method. For example, the following markup displays the Address
model with the AddressShort
template:
Use one of the available DisplayFor overloads that expose the additionalViewData
parameter to pass additional view data that is merged into the View Data Dictionary instance created for the template.
Editor templates
Editor templates are used in form controls when the model is edited or updated.
An EditorTemplate
is a Razor file placed in theEditorTemplates
folder:
- For Razor Pages apps, in the
Pages/Shared/EditorTemplates
folder. - For MVC apps, in the
Views/Shared/EditorTemplates
folder or theViews/ControllerName/EditorTemplates
folder.
The following markup shows the Pages/Shared/EditorTemplates/Address.cshtml
used in the sample:
The following markup shows the Edit.cshtml page which uses the Pages/Shared/EditorTemplates/Address.cshtml
template: