Copied to clipboard

Flag this post as spam?

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


  • Morten Eberhardt Lund 17 posts 107 karma points
    Jan 20, 2017 @ 12:26
    Morten Eberhardt Lund
    0

    Foreach loop on onepage

    I got this onepage that I'm working on.

    On the onepage, I have a section with a gallery with images. The images is links to subpages.

    My site

    I tried this:

    @foreach (var CaseUnderPage in CurrentPage.Children)
    {
       <div>
         <a href="@CaseUnderPage.Url">
            <img src="@CaseUnderPage.CaseImage">
         </a>
       </div>
    }
    

    ... It works, but when I'm adding the code again for another section, the code is taking every subpage in the root.

    How am I gonna write this, so that it only takes the images in the subpage folder?

  • Ayo Adesina 445 posts 1059 karma points
    Jan 20, 2017 @ 12:36
    Ayo Adesina
    0

    If I understand you correctly its working but you are getting all the nodes?

    What you need to do is add a where clause and a lambda expression. Filtering out the other document types

    Like so...

      foreach (var CaseUnderPage in CurrentPage.Where(x => x.DocumentTypeAlias ="YourDocTypeAliasForImagesHere")
                                                {
    

    Hope that helps....

  • Morten Eberhardt Lund 17 posts 107 karma points
    Jan 20, 2017 @ 13:20
    Morten Eberhardt Lund
    0

    Hi Ayo,

    I follow your thoughts with the filtering.

    I'm just not sure, if I'm doing this right, because it's not working :(

    enter image description here

  • Ayo Adesina 445 posts 1059 karma points
    Jan 20, 2017 @ 13:26
    Ayo Adesina
    0

    you don't need node bit....

    just CurrentPage.Where

    remove

    node.

  • Morten Eberhardt Lund 17 posts 107 karma points
    Jan 20, 2017 @ 13:44
    Morten Eberhardt Lund
    0

    Hi Ayo,

    That leads me to this problem.

    Sorry, I'm not used to work with Umbraco that much.

    enter image description here

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 20, 2017 @ 13:41
    Dirk De Grave
    0

    Just a little note....

    .Where(x => x.DocumentTypeAlias ="YourDocTypeAliasForImagesHere")
    

    isn't ok, you need to use the == comparison operator, = is an assigment operator

    .Where(x => x.DocumentTypeAlias == "YourDocTypeAliasForImagesHere")
    

    --Dirk

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 20, 2017 @ 13:47
    Dirk De Grave
    100

    If you want to use this piece of code anywhere in the site, you'd probably have to make sure to get to the correct node (Cases) in your tree structure, regardless of the current page.

    So, if you'd have a single root node "Home", you're best bet is to use

    var caseNode = Model.Content.AncestorOrSelf("HomeAlias").Children.First(x => x.IsDocumentType("CasesAlias")
    

    and then use a foreach loop

    foreach(var childNode in caseNode) {...}
    

    I'm using typed objects here, CurrentPage is a dynamic object, so use whatever you prefer... but I'm sure you get the gist.

    -Dirk

  • Morten Eberhardt Lund 17 posts 107 karma points
    Jan 20, 2017 @ 14:12
    Morten Eberhardt Lund
    0

    Hi Dirk,

    Thank you for the response, I've tried to work with your solution here, but I'm still kinda running into problems, am I doing something wrong?

    I hope you can read it.

    enter image description here

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 20, 2017 @ 14:15
    Dirk De Grave
    1

    Yup, and probably my mistake.

    foreach(var childNode in caseNode.Children) { ... }
    

    --Dirk

  • Morten Eberhardt Lund 17 posts 107 karma points
    Jan 20, 2017 @ 14:21
    Morten Eberhardt Lund
    0

    Hi Dirk,

    Haha, no props man. This seem to work, but now I seem to have a new problem.

    It can't find my images that's uploaded in the underpages of Cases.

    enter image description here

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 20, 2017 @ 14:25
    Dirk De Grave
    0

    Of course I don't have any info on your "image" content, so bit hard to guess, but the problem here is that you're trying to work with dynamic "syntax" again, while my example is using typed IPublishedContent objects, so if your property on the image is called "caseImage", you'd need to use

    @childNode.GetPropertyValue<string>("caseImage")
    

    to get the path of the image.

    -Dirk

  • Morten Eberhardt Lund 17 posts 107 karma points
    Jan 20, 2017 @ 14:43
    Morten Eberhardt Lund
    0

    Hi Dirk

    I'm know, sorry, I'm a little bad at explaining, so I'll try to illustrate it now.

    enter image description here

    enter image description here

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 20, 2017 @ 14:45
    Dirk De Grave
    0

    Ok, just change

    @childNode.GetPropertyValue<string>("caseImage")
    

    into

    @(childNode.GetPropertyValue<string>("caseImage"))
    

    --Dirk

  • Morten Eberhardt Lund 17 posts 107 karma points
    Jan 20, 2017 @ 14:50
    Morten Eberhardt Lund
    0

    YES!

    Thanks a lot for your time, Dirk!

    It works!

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 20, 2017 @ 14:27
    Dirk De Grave
    1

    Here's a good reference on typed vs dynamic, in case you want to have a better understanding. I'm not forcing you to use this or any, just that you know there's different ways to do it in Umbraco (I'm on the typed side if you'd wonder)...

    http://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/

Please Sign in or register to post replies

Write your reply to:

Draft