Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Linus Ekström
Apr 10, 2014
  4155
(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
Level Up with Optimizely's Newly Relaunched Certifications!

We're thrilled to announce the relaunch of our Optimizely Certifications—designed to help partners, customers, and developers redefine what it mean...

Satata Satez | Jan 14, 2025

Introducing AI Assistance for DBLocalizationProvider

The LocalizationProvider for Optimizely has long been a powerful tool for enhancing the localization capabilities of Optimizely CMS. Designed to ma...

Luc Gosso (MVP) | Jan 14, 2025 | Syndicated blog

Order tabs with drag and drop - Blazor

I have started to play around a little with Blazor and the best way to learn is to reimplement some old stuff for CMS12. So I took a look at my old...

Per Nergård | Jan 14, 2025

Product Recommendations - Common Pitfalls

With the added freedom and flexibility that the release of the self-service widgets feature for Product Recommendations provides you as...

Dylan Walker | Jan 14, 2025