Created
September 21, 2015 14:58
-
-
Save jamiepollock/fc69044cf874bd838d76 to your computer and use it in GitHub Desktop.
Custom Ditto ValueResolver - Single IPublishedContent from IEnumerable<IPublishedContent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using Our.Umbraco.Ditto; | |
using Umbraco.Core.Models; | |
namespace My.Website.ComponentModel | |
{ | |
public class SinglePublishedContentUmbracoPropertyAttribute : UmbracoPropertyAttribute | |
{ | |
} | |
public class SinglePublishedContentValueResolver : UmbracoPropertyValueResolver | |
{ | |
public override object ResolveValue() | |
{ | |
var value = base.ResolveValue(); | |
if (value is IEnumerable<IPublishedContent>) { | |
return (value as IEnumerable<IPublishedContent>).FirstOrDefault(); | |
} | |
throw new InvalidCastException(string.Format("Invalid value resolver! The value must be of type {0}", typeof(IEnumerable<IPublishedContent>))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found this while Googleing and didn't have any context. For future Googlers, this is the thread that this code was born from:
https://our.umbraco.org/projects/developer-tools/ditto/ditto-feedback/71581-ienumerableipublishedcontent-to-single-ipublishedcontent-dittovalueresolver-or-typeconverter