Search code examples
c#razorumbraco

ContentPicker Return null


I'm trying to get the selected Content with my ContentPicker but when I debug the code. It just returns a null in my ContentPicker.

My DocumentType enter image description here

ContactPerson.cs

using System.Linq;
using umbraco.controls;
using Umbraco.Core.Models;
using Umbraco.Site.Extensions.Extensions;
using Umbraco.Site.Extensions.Models.Media;
using Umbraco.Web;

namespace Umbraco.Site.Extensions.Models.DocumentTypes.Grid
{
    public class ContactPerson
    {
        public ContentPicker contentPicker { get; set; }

        public ContactPerson(IPublishedContent content)
        {
            contentPicker = content.GetPropertyValue<ContentPicker>("contactPersonPicker");
        }
    }
}

ContactPerson.cshtml

@using Umbraco.Site.Extensions.Models.DocumentTypes.Grid
@inherits UmbracoTemplatePage
@{
    Layout = null;
    ContactPerson cp = new ContactPerson(Model.Content);

    string s = "s";
}

<div>test</div>

Note: the only reason why i have string s, is to debug.


Solution

  • ContentPicker by default returns ID of selected content node. Try to get the string first and see if value is there. If not - republish content or the whole website to force XML cache to rebuild.

    So, it's not so obvious to map it to ContentPicker class and it requires additional properties mostly focues on property editor, not value itself.

    Basic usage of ContentPicker is here: https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/content-picker. And about it I was writing above at the beggining.

    I can also suggest you to use (install is possible even via NuGet) Umbraco Core Property Value Converters package: https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters. It's planned to include this inside the Umbraco Core and it's basically mapping values of common data types into more convenient and usable ones e.g. ContentPicker value will be IPublishedContent model of selected document, ready and easy to use at document where it's selected.