ZauberCMS
  • 🪄Welcome to ZauberCMS
  • Getting Started
    • 💡Quick Start
      • Databases
  • Creating A Website
    • 📹Website Build Video Tutorial
    • Content Types
      • Element Types
      • Compositions
    • Current Content Properties
      • Textbox & Textarea
      • Text Editor (Radzen)
      • Text Editor (TinyMCE)
      • Numeric
      • True / False
      • Rating
      • Checkbox, Dropdown & Radio Lists
      • Media Picker
      • Navigation
      • Material Icon Picker
      • Content Picker
      • Date Picker
      • Custom Component Picker
      • Api Key Picker
      • Colour Picker
      • Block List Editor
      • Editor Notes
      • Google Map Location Picker
      • Simple List Manager
      • Simple Dictionary Manager
      • SEO Property
      • Code Editor
      • Colour Theme Picker
    • Content
      • Publish & Unpublish
    • Querying Data
      • Extension Methods
    • Views
    • Controllers (Route Hi-Jacking)
    • Custom Components
    • Users & Roles
      • Restrict Access By Role
    • Logs
    • Audit
    • Global Settings
      • Using Global Settings
    • SEO Sitemaps
    • Hosting
  • Extending ZauberCMS
    • Overview
    • BlockListEditor
      • Content Block Preview
      • Content Block
    • Custom List Data
    • Custom Content Property
    • Custom Validation
    • Custom Admin Sections
      • Section Nav Group Menu
      • Trees
        • Tree Context Menus
      • Reusing Content Editors
    • Saving Custom Data
    • Using AppState Events
    • Before & After Save
    • Email & Storage Providers
    • Seed Data
    • SEO Checks
  • Identity
    • Overview
    • External Authentication Providers
    • Account Layout
  • Language
    • Overview
    • Adding Language Dictionaries
    • Setting The Language
    • Using Language Dictionaries
  • AppSettings
    • Detailed Errors
    • Media Settings
    • Enable Path Urls
Powered by GitBook
On this page
  • Naming Conventions
  • View Location
  • Custom View Models
  • CurrentPage
  1. Creating A Website

Controllers (Route Hi-Jacking)

* Controllers are completely optional, but recommended to make the most out of the CMS

If you want to have your own Model passed to a View it works exactly like a normal MVC app. We can create a controller, and a custom View Model and pass these to the View. You just have to adhere to naming conventions.

Naming Conventions

The naming convention is pretty simple. The controller name, must match your 'Content Type' alias, and the ActionResult name must match the View you are wanting to use. Also, all controllers need to inherit from

ZauberRenderController

Example below shows the Home Content Type Controller from the starter site.

/public class HomeController(ILogger<HomeController> logger, IOptions<ZauberSettings> options, IMediator mediator) 
    : ZauberRenderController(logger, options, mediator)
{
    private readonly IMediator _mediator = mediator;

    /// <summary>
    /// Route hijacked controller, 'Home' ContentType and 'Home' View 
    /// </summary>
    /// <returns></returns>
    public async Task<IActionResult> Home()
    {
        var homeViewModel = new HomeViewModel(CurrentPage!)
        {
            // Get the header image
            HeaderImage = await CurrentPage!.GetMedia("HeaderImage", _mediator, "/assets/img/home-bg.jpg")
        };

        // Get the blog posts
        var posts = await _mediator.Send(new QueryContentCommand
        {
            AmountPerPage = 4,
            ContentTypeAlias = "BlogPage",
            OrderBy = GetContentsOrderBy.DateUpdatedDescending,
            Cached = true
        });

        homeViewModel.BlogPosts = posts.Items.ToList();
        
        return CurrentView(homeViewModel);
    }
}

As you can see, we have a custom view model called HomeViewModel and this is passed to the View using CurrentView() which is also important.

Using the View name as the ActionResult, means you can have different ActionResults for each View, so for differently layouts you could (Optionally) pass in

View Location

By default the Views are searched for in the Views folder route, but if you want to keep the normal MVC Views convention you can. So for the Home Controller, you can also put your views in /Views/Home/ if that's how you like to organise your views.

Custom View Models

Custom View Models must inherit from

ZauberPageViewModel

The example below, shows the full view model for the Home View Model used above in the controller.

public class HomeViewModel(Content content) : ZauberPageViewModel(content)
{
    public Media? HeaderImage { get; set; }
    public List<Content> BlogPosts { get; set; } = [];
}

CurrentPage

CurrentPage gets the Content model for the current page you are on.

PreviousViewsNextCustom Components

Last updated 3 months ago