Search code examples
c#razorumbraco

Multiple content types in one template with Umbraco.ModelsBuilder


We have multiple base content types News, Reviews. Each inherit properties from our composites: _Page & _Article. This allows us to wack all three content types into a single article template.

Currently using IPublishedContent, means we have something like:

    if (Model.Content.DocumentTypeAlias == "review") 
    { 
        // ... Do This 
    }

With ModelsBuilder, we can't do this anymore.

The result is models looking like this:

UML

Tried to do: @inherits UmbracoViewPage<Umbraco.Web.PublishedContentModels.IArticle>, but this means I lose all my other properties.

What model would you inherit for a template using multiple content types, or is there something else we need to do?

A note on this: I would love to do _Page > _Article > ContentType, but it would mean huge data loss.


Solution

  • If you do @inherits UmbracoViewPage<Umbraco.Web.PublishedContentModels.IArticle>, then you could do something like var review = Model as Umbraco.Web.PublishedContentModels.Review. if review is not null, you know that the page is of type Review.

    (By the way, cant remember if it needs to be Model.Content you do the cast on.)