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);
}
}
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.
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:
Code snippet below.
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:
*It actually uses Property Value Converters
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
is working on a reply...