Skip to content

Instantly share code, notes, and snippets.

@abjerner
Last active August 29, 2015 14:07
Show Gist options
  • Save abjerner/36ed6d19127610f7d1e3 to your computer and use it in GitHub Desktop.
Save abjerner/36ed6d19127610f7d1e3 to your computer and use it in GitHub Desktop.
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);
}
}
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;
}
}
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);
}
}
@{
// 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);
}
@abjerner
Copy link
Author

abjerner commented Oct 3, 2014

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