Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Linus Ekström
Apr 10, 2014
  3889
(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
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

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