Displaying shared content in MVC applications

You can display the reusable content added by content editors in your MVC application’s views. The shared content, created by adding pages in the Pages form control, is accessible via a field that’s part of the code generated for individual page types.

To display reused content in your MVC application

  1. Perform the initial set up of your MVC application.

  2. Reference the page types that you want to access in your view using the @model directive:

    
    
    
     @using CMS.DocumentEngine.Types;
     @model Article
     ...
    
    
     
  3. Access the individual related pages stored in the view.
    Note that by default, the pages are accessed in the same order in which they are added by content editors on the Form tab.

    
    
    
     // Create a list of individual pages stored in the 'Pages' page type field named 'ContentItems'
     @{ var pages = Model.Fields.ContentItems.ToList(); }
    
     @if (pages.Any())
     {
         ...
         // Iterate over each of the pages of a specific type in the same order in which they are added on the Form tab
         @foreach (var article in pages.OfType<Article>())
         {
             ...
             // Display the image stored in the page's 'Teaser' field
             <img src="@Url.Kentico().Attachment(article.Fields.Teaser) class="article-tile-image" />
             ...
         }
     }
    
    
     

See our MVC Demo site for a showcase of using this approach to display an article containing content added via the Pages field.