Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Søren Mastrup 122 posts 564 karma points c-trib
    Oct 05, 2015 @ 08:02
    Søren Mastrup
    0

    Getting nested content in groups?

    I am trying to get my nested content in groups, but I can't figure out how to do this. This is my loop:

    @foreach(var logos in CurrentPage.Site().GetPropertyValue<IEnumerable<IPublishedContent>>("footerlogo").InGroupsOf(2)){
        @logos
    }
    

    The code above returns the following error:

    System.Collections.Generic.List<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'InGroupsOf
    

    How would i render my nested content in groups?

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Oct 05, 2015 @ 10:33
    Tim
    1

    If you add a @using Umbraco.Core statement to the top of the view, does it work?

    If not, which version of Umbraco are you using please?

  • Søren Mastrup 122 posts 564 karma points c-trib
    Oct 06, 2015 @ 06:11
    Søren Mastrup
    0

    I've tried that. It didn't work.

    EDIT: Forgot to mention, that I am running 7.2.8

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 06, 2015 @ 20:58
    Lee Kelleher
    0

    I was going to suggest the same thing as Tim. The InGroupsOf is an extension method within the "Umbraco.Core" namespace, so it's weird that the error is being thrown.

    I'm wondering if it work with any other IEnumerable<T>?

    e.g. var groups = Enumerable.Range(1, 10).InGroupsOf(2);

    See what happens then?

    Cheers,
    - Lee

  • Søren Mastrup 122 posts 564 karma points c-trib
    Oct 07, 2015 @ 10:08
    Søren Mastrup
    0

    That works!

    var groups = Enumerable.Range(1, 10).InGroupsOf(2);
    foreach(var demo in groups){
        <br>
        foreach(var test in demo){
            @test
        }
    }
    

    This is my Stack Trace for the example in the first post of this thread:

    [RuntimeBinderException: 'System.Collections.Generic.List<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'InGroupsOf']
       CallSite.Target(Closure , CallSite , Object , Int32 ) +232
       System.Dynamic.UpdateDelegates.UpdateAndExecute2(CallSite site, T0 arg0, T1 arg1) +991
       ASP._Page_Views_Partials_Footer_cshtml.Execute() in e:\dynamicweb.net\oerskovweb.dk\Views\Partials\Footer.cshtml:60
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
       System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +196
       Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) +140
       System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +147
       ASP._Page_Views_Master_cshtml.Execute() in e:\dynamicweb.net\oerskovweb.dk\Views\Master.cshtml:53
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
       System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +196
       System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +208
       System.Web.WebPages.WebPageBase.PopContext() +195
       Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) +140
       System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +380
       System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +33
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +263
       System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
       System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
       System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
       System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
    
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 07, 2015 @ 11:13
    Lee Kelleher
    0

    Hi Søren,

    Hmmm, it's a bit confusing why InGroupsOf would work for the number range, but not for the node list.

    Does the loop work if you remove the InGroupsOf(2) call from it?

    foreach(var logos in CurrentPage.Site().GetPropertyValue<IEnumerable<IPublishedContent>>("footerlogo")
    

    Does that throw an error?

    After that, I'm not sure what else I can suggest :-(

    Thanks,
    - Lee

  • Søren Mastrup 122 posts 564 karma points c-trib
    Oct 07, 2015 @ 11:18
    Søren Mastrup
    0

    Hi Lee

    Yes, that would work. This:

    @foreach(var logos in CurrentPage.Site().GetPropertyValue<IEnumerable<IPublishedContent>>("footerlogo")){
        @logos<br>
    }
    

    Would return this:

    Our.Umbraco.NestedContent.Models.DetachedPublishedContent
    Our.Umbraco.NestedContent.Models.DetachedPublishedContent
    Our.Umbraco.NestedContent.Models.DetachedPublishedContent
    Our.Umbraco.NestedContent.Models.DetachedPublishedContent
    
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 07, 2015 @ 11:26
    Lee Kelleher
    0

    Hmmm, I'm still confused about the "'System.Collections.Generic.List<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'InGroupsOf'" error.

    As the node should apply to any IEnumerable<T> extension method.

    I'm wondering what would happen if you tried this...

    var groups = Enumerable.Range(1, 10).ToList().InGroupsOf(2);
    

    ...

    Feels like I'm clutching at straws now though :-(

    Cheers,
    - Lee

  • Søren Mastrup 122 posts 564 karma points c-trib
    Oct 07, 2015 @ 11:34
    Søren Mastrup
    0
    var groups = Enumerable.Range(1, 10).ToList().InGroupsOf(2);
    

    This would return the same as your first suggestion.

    If apply this to my code, I get the following error:

    'System.Collections.Generic.List<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'ToList'
    
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 07, 2015 @ 11:44
    Lee Kelleher
    100

    Sorry, I wasn't trying to suggest using ToList() on an existing List<T>.

    Hmmm, ok, you could try doing it the other way around, calling the extension method directly...

    var logos = CurrentPage.Site().GetPropertyValue<IEnumerable<IPublishedContent>>("footerlogo");
    var groups = Umbraco.Core.EnumerableExtensions.InGroupsOf(logos, 2);
    

    Then loop over the groups.

  • Søren Mastrup 122 posts 564 karma points c-trib
    Oct 08, 2015 @ 09:35
    Søren Mastrup
    0

    That did the trick!

    Thank you Lee!

    alt text

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 08, 2015 @ 09:37
    Lee Kelleher
    0

    LOL! Love the gif!

    I still find it weird that the extension method didn't work (from your first code snippet), but glad that we eventually got there! :-)

    Cheers,
    - Lee

Please Sign in or register to post replies

Write your reply to:

Draft