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);
}
@JimBobSquarePants
Copy link

Kinda....

What I'm working on at the moment would be a little bit more automated.

public class SubPage: PublishedContentWrapped {

  public:SubPage(IPublishedContent content)
   : base(content)
  {
  }

  public int Id { get; set; }

  public string Name { get; set; }

  public string Url { get; set; }

  public string Text { get; set; }   

}

public class  ParentPage: PublishedContentWrapped {

  public ParentPage(IPublishedContent content)
  : base(content)
  {
  }

   [TypeConverter(typeof(MultiNodeTreePickerConverter))] 
  public IEnumerable<SubPage> SubPages { get; set; }    
}

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.

@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