Try our conversational search powered by Generative AI!

Linus Ekström
Apr 10, 2014
  3882
(2 votes)

Property Renderers and nullable types

Today I got a question from a collegue that could not get a specific property template for a DateTime property to be used. The template resolver never selected this renderer for some reason, even if the tags was matching. Since I’ve seen other people having the same issue I thought I’d give a short description to what the problem is and also how to fix it.

The property declaration in the model looked like this:

public virtual DateTime OriginallyPublished { get; set; }

And the property template like this:

[TemplateDescriptor(TagString = "ArticleDate", TemplateTypeCategory = TemplateTypeCategories.ServerControl)]
    public partial class ArticleDate : PropertyControlBase<DateTime>
    {
        protected override void CreateChildControls()
        {
            Controls.Add(new Label() { Text = "Just some text to test" });
        }
    }

The usage on the template looked like this:

<EPiServer:Property PropertyName="OriginallyPublished" CustomTagName="div" runat="server">
    <RenderSettings Tag="ArticleDate" />
</EPiServer:Property>

Now given the theory how templates are selected given the type and tags, one could think that the ArticleDate class would be selected since the property is of type DateTime and the Tags defined for rendering matches the TagString on the TemplateDescriptor. Still, this template is not selected. Why then is that the case?

Nullable properties

The answer is that EPiServer stores some simple value types as a nullable type, for instance Nullable<DateTime>. We still allow model declarations being non-nullable, setting the default value for the type if the backing value is null. However, when matching value type with templates, we use the internall data type. So the quick fix is simply to add nullable to your template:

public partial class ArticleDate : PropertyControlBase<Nullable<DateTime>>

I have reported a bug to see if we can handle this oddity in a transparant way so that you don’t have to care about this as a site developer.

Apr 10, 2014

Comments

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog