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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.