Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jason 92 posts 175 karma points
    Apr 19, 2016 @ 14:47
    Jason
    0

    Returning 'Content Picker' value as IPublishedContent

    I ran in to a problem earlier, where I wanted to create a strongly-typed model (HomeViewModel), containing 'CustomModel' properties, populated by a ContentPicker.

    I thought it'd be worth sharing with the community, should anyone else encounter the problem.

    This may not be the most elegant solution out there, but it's easy to understand and works.

    The problem began when I wanted to have properties of another custom model (MagazineViewModel). I wanted to populate my MagazineViewModel with properties from the ContentPicker value returned on my HomeViewModel. As we know, the value returned from a ContentPicker is a node ID.

    I wrote a simple extension method on IPublishedContent that first gets the property value and returns an IPublishedContent object from the cache. This means that in my view I can:

    • Access HomeViewModel properties like Model.x
    • Access Magazines (ContentPicked) from my HomeViewModel like Model.MagazineName.x

    Code snippet below.

        public HomeViewModel(IPublishedContent Content)
            : base(Content)
        {
        }
    
        public string SliderImages
        {
            get { return Content.GetPropertyValue<string>("mainImageSlider"); }
        }
    
        public string IntroText
        {
            get { return Content.GetPropertyValue<string>("introText"); }
        }
    
        public MagazineViewModel InEnglishMagazine
        {
            get
            {
               return new MagazineViewModel(Content.GetPropertyValueAsIPublishedContent("inEnglishMagazine"));
            }
        }
    
        public MagazineViewModel InStepsMagazine
        {
            get { return new MagazineViewModel(Content.GetPropertyValueAsIPublishedContent("inStepsMagazine")); }
        }
    }
    
    
    public class MagazineViewModel
    {
        public MagazineViewModel(IPublishedContent Content)
        {
            Image = Content.GetPropertyValue<string>("image");
            FlashLink = Content.GetPropertyValue<string>("flashMagazine");
            MobileLink = Content.GetPropertyValue<string>("mobileMagazine");
        }
    
        public string Image { get; private set; }
        public string FlashLink { get; private set; }
        public string MobileLink { get; private set; }
    }
    
    
    public interface ICanBeMobile
    {
    
    }
    
    
    
    public static class PublishedContentExtension
    {
        public static IPublishedContent GetPropertyValueAsIPublishedContent(this IPublishedContent Content, string Alias)
        {
            var Id = Content.GetPropertyValue<int>(Alias);
            return UmbracoContext.Current.ContentCache.InnerCache.GetById(UmbracoContext.Current, false, Id);
        }
    }
    
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 19, 2016 @ 18:47
    Dan Diplo
    1

    Have a look at this great package:

    https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/

    It allows you to "magically"* return IPublishedContent from pickers; then you can simply go:

    var page = Model.Content.GetPropertyValue<IPublishedContent>("contentPickerAlias");
    

    *It actually uses Property Value Converters

  • Jason 92 posts 175 karma points
    Apr 20, 2016 @ 09:13
    Jason
    0

    Hi Dan, thanks for your reply. I didn't want to install a package in this case as I only wanted the functionality on a Content Picker only. I must admit that I didn't come across the one you suggested. I did however find one on GitHub, but this changed the syntax for obtaining 'standard' values, which I didn't feel appropriate.

    Interesting to see the similarities here though: https://our.umbraco.org/Documentation/Extending/Property-Editors/value-converters-full-example-attributes

    Thanks again

Please Sign in or register to post replies

Write your reply to:

Draft