Rendering BlockList items with ModelBuilders: None?

Following the V15 documentation on creating a display template for my blocklist I have come up with this simple code:

@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockListItem>;
@using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;

@{ var content = (ContentModels.CoveredMediaItemBlock)Model.Content; }

<div id="coveredMeditaItem" class="centerText">
    <p>
        <picture>
            <source media="(min-width: 1024px)" srcset="@Url.GetCropUrl(content.MediaItemLogo, width: 423, height: 238, imageCropMode: ImageCropMode.Max)">
            <source media="(max-width: 1023px)" srcset="@Url.GetCropUrl(content.MediaItemLogo, width: 200, height: 120, imageCropMode: ImageCropMode.Max)">
            <img src="" alt="@content.MediaItemTitle Logo" decoding="async" />
        </picture><br />
        <a href="@content.MediaItemLink?.Url">@content.MediaItemTitle</a>
    </p>
</div>

When I preview the page that has this block list on it I get the following error:

The type or namespace name ‘CoveredMediaItemBlock’ does not exist in the namespace ‘Umbraco.Cms.Web.Common.PublishedModels’ (are you missing an assembly reference?)

On my local dev machine this works fine, but that is because I have ModelBuildres: SourceCodeAuto set. But up in production it is set to None. I used uSync to move the document types, block list, and templates up to production but because I did not deploy my code the model CS files is not there. Looking at the docuementation it seems like my code should work without ModelsBuilder but the code breaks. What am I missing to get this to work without the model CS file exising or is block lists on of those things where I must build and deploy to get it working?

Hi! If you look at the Models Builder documentation you’ll see in this in the settings:

SourceCodeAuto : Generate models in ~/umbraco/models (but do not compile them) anytime a content type changes.

This means to get the models into the build you need to compile them in whatever deploy process you’re using :slight_smile:

Thanks but as stated I am trying to get the blocks to render without having to deploy the model. With normal document types you can do it using the Value() method. But from the looks of the documents you must cast the blocklist into a model but I have also found that the documentation is very poor at providing code examples so I am hoping they forgot to provide an example and I am missing how to display a blocklist without having to deploy the models. Otherwise this wold mean that everytime I want to make a new blocklist I have to take the site offline for up to 10 minutes while a new deploy, build, warmup, and reindex happens for the site.

Yep, okay so I think you’ll want to set it to InMemoryAuto, but if you don’t want to deploy it and want to use the strongly typed models on the site then you’ll want to turn this on where you’re hosting as well - this way it will update the models when you’ve imported uSync and it’s saved the DocTypes.

You can alternately use Model.Content.Value() like you would on a document without casting it to the typed model.

Example:

@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockListItem>;

@{ 
    var content = Model.Content;
    var logo = content.Value<IPublishedContent>("mediaItemLogo");
    var title = content.Value<string>("mediaItemTitle");
    var link = content.Value<Link>("mediaItemLink");
}

<div id="coveredMeditaItem" class="centerText">
    <p>
        <picture>
            <source media="(min-width: 1024px)" srcset="@Url.GetCropUrl(logo, width: 423, height: 238, imageCropMode: ImageCropMode.Max)">
            <source media="(max-width: 1023px)" srcset="@Url.GetCropUrl(logo, width: 200, height: 120, imageCropMode: ImageCropMode.Max)">
            <img src="" alt="@title Logo" decoding="async" />
        </picture><br />
        <a href="@link?.Url">@title</a>
    </p>
</div>

Thanks, that worked almost perfictly. I had to att @using Umbraco.Cms.Core.Models for the ImageCropMode.Max to work and then needed to make a custom date type based off the Muti-Link Pickers and set the min/max to 1. Once those two things where done this worked perfectly. Thank you for the help