November Happy Hour will be moved to Thursday December 5th.

Linus Ekström
Apr 10, 2014
  4102
(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
Adding Geolocation Personalisation to Optimizely CMS with Cloudflare

Enhance your Optimizely CMS personalisation by integrating Cloudflare's geolocation headers. Learn how my Cloudflare Geo-location Criteria package...

Andy Blyth | Nov 26, 2024 | Syndicated blog

Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog