Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Expand]WinForms Controls
[Collapse]ASP.NET Controls and MVC Extensions
 [Expand]Prerequisites
 [Expand]What's Installed
 [Expand]Common Concepts
 [Expand]ASP.NET WebForms Controls
 [Collapse]ASP.NET MVC Extensions
  [Expand]Product Information
  [Expand]Getting Started
  [Collapse]Common Concepts
    Web Application Project Templates
    Insert Extension Wizard
    Data Binding
   [Expand]Applying Themes
    Client-Side API
    Declaring Server-Side Event Handlers
    Templates
    Using Callbacks
    Passing Values to a Controller Action through Callbacks
    Supported Attributes for Model Validation
    Using Extensions in Razor Views
  [Expand]Grid View
  [Expand]Card View
  [Expand]Vertical Grid
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]HTML Editor
  [Expand]Image and Data Browsing
  [Expand]Docking and Popups
  [Expand]Navigation and Layout Extensions
  [Expand]File Management
  [Expand]Multi-Use Site Extensions
  [Expand]Charts
  [Expand]Pivot Grid
  [Expand]Scheduler
  [Expand]Tree List
  [Expand]Data Editor Extensions
  [Expand]Report Extensions
  [Expand]SpellChecker
 [Expand]Localization
 [Expand]Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Templates

Templates allow you to quickly extend built-in functionality and introduce new capabilities with minimum effort. The DevExpress MVC extensions provide you with the ability to create templates for various extension elements.

Templates can be defined at different object levels at the same time (e.g., at the extension level and the item level). In this case, templates created at a lower level in the logical tree take precedence over other templates for the same element created at a higher level (e.g., a template defined at the item level has precedence over the same element template created at the extension level).

The SetElementNameTemplateContent methods are provided to define an element template.

Expanded Defining templates in partial view pages

For each template type you want to use, create a Partial View (a .cshtml or .vbhtml) and provide it with the required content markup. To use this Partial View within a template, call the Html.RenderPartial helper method in template implementation.

Note that the method delegates that enable you to implement templates give you access to a container object within which a template is instantiated. A template container object typically exposes useful information identifying the templated element (for instance, for a grid column header these might be the corresponding column, the header's current location - in the header section, Group Panel, or Customization Window, etc.). Depending upon your application logic, you may or may not use this information when composing templates. The following options are available.

  • Without Passing a Template Container

    If a template is simple and its content does not depend on the information provided by a template container, you do not have to pass a template container object to a partial view. You can define the template contents by using a HTML markup.

    View:

    Partial View ("MyMenuItem"):

    Note that you can do this more easily by using another method overload that accepts a string as a parameter.

    View:

  • Passing a Template Container as a Model to a Partial View

    If you dynamically compose a template based on template container information, you can pass a template container object to a partial view page as a Model. Use the Html.RenderPartial method's model parameter for this purpose.

    View:

    Partial View ("MyMenuItem"):

  • Passing Template Container via ViewData

    You can also pass a template container object to a partial view page as a part of the ViewData by using the Html.RenderPartial method's viewData parameter.

    View:

    Partial View ("MyMenuItem"):

Expanded Defining templates in delegate methods directly

If you do not want to use partial views for some reason, and need to entirely define template contents within a delegate method implementation, you can do this using the following options.

  • Using DevExpress extensions as child controls

    You can safely compose a template with the use of DevExpress extensions, which can be rendered with either the ExtensionBase.Render or ExtensionBase.GetHtml method.

    View:

  • Using ViewContext.Writer.Write

    If you need to use expressions and an HTML markup, and then combine them with server code within template contents, you can embed a HTML markup by using the ViewContext.Writer.Write method.

    View:

  • Using HtmlHelper class

    You can implement an extension method for the HtmlHelper in order to access it in the Controller (not a View) scope. To implement this functionality, you may pass an HtmlHelper object to some static method that creates DevExpress extension settings with templates. At first, you may create some class to get extension settings shared between Views.

    Controller code:

    After writing methods to get the settings, you may use this class in various Partial Views as your needs dictate.

    Partial view code (ASPX):

How would you rate this topic?​​​​​​​