Last active
August 29, 2015 14:07
-
-
Save abjerner/36ed6d19127610f7d1e3 to your computer and use it in GitHub Desktop.
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
public class ParentPage { | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Url { get; set; } | |
public IEnumerable<TestPage> Pages { get; set; } | |
public ParentPage(IPublishedContent content) { | |
Id = content.Id; | |
Name = content.Name; | |
Url = content.Url; | |
Pages = content.TypedCsvContent("somePages", TestPage.GetFromContent); | |
} | |
public static ParentPage GetFromContent(IPublishedContent content) { | |
return content == null ? null : new ParentPage(content); | |
} | |
} |
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
public class PublishedContentExtensionMethods { | |
public static IEnumerable<T> TypedCsvContent<T>(this IPublishedContent content, string propertyName, Func<IPublishedContent, T> func) { | |
int[] ids = content.GetPropertyValue<string>(propertyName).CsvToInt(); | |
List<T> result = new List<T>(); | |
foreach (int id in ids) { | |
try { | |
IPublishedContent item = UmbracoContext.Current.ContentCache.GetById(id); | |
if (item != null) result.Add(func(item)); | |
} catch (Exception) { | |
} | |
} | |
return result; | |
} | |
} |
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
public class TestPage { | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Url { get; set; } | |
public string Text { get; set; } | |
public TestPage(IPublishedContent content) { | |
Id = content.Id; | |
Name = content.Name; | |
Url = content.Url; | |
Text = content.Text; | |
} | |
public static TestPage GetFromContent(IPublishedContent content) { | |
return content == null ? null : new TestPage(content); | |
} | |
} |
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
@{ | |
// Parse the parent content node | |
ParentPage parent = ParentPage.GetFromContent(Model.Content); | |
// Iterate through the selected pages | |
foreach (var page in parent.Pages) { | |
} | |
// The property could hold a CSV with IDs for each selected page of something (eg. from the MNTP) | |
IEnumerable<TestPage> pages = Model.Content.TypedCsvContent("somePages", TestPage.GetFromContent); | |
} |
I'm haven't really had the time to research the different model builders, so I still prefer to create the models somewhat manual. So I don't think I can help you with something automatic.
Even though you're searching for something automatic, I've updated the Gist to shown how I would accomplish it using a manual approach.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kinda....
What I'm working on at the moment would be a little bit more automated.
It works... Kinda... when I use an extension method from here. https://github.com/leekelleher/umbraco-ditto/blob/master/src/Our.Umbraco.Ditto/Extensions/PublishedContentExtensions.cs
I've created a type converter from MNTP to aid with conversion but I don't want to have to add the type converter attribute. I was hoping there would be a more consistent data structure coming in from the various types.
It looks like I'm going to have to write a lot more code than I was hoping.