Copied to clipboard

Flag this post as spam?

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


  • Matt 353 posts 825 karma points
    Nov 17, 2018 @ 17:42
    Matt
    0

    Display content under folders

    Hello,

    So I'm create a simple products page with Umbraco here is my tree;

    enter image description here

    I have a stock page which renders the following sub folders, "Lamp" and "Chairs" which will have some products under it.

    enter image description here

    For the sub folders I create a doctype with a template called "StockSubCat"

    what I'm having issues with is rendering the content under the sub folders. So I want it if I click on Lamps, then all the products will show. I thought this code would work but I just get a blank page?

    @foreach(var subCat in Model.Content.Children().Where(c=>c.DocumentTypeAlias == "StockSubCat"))
      {
          <div>@subCat.Name</div>
          <ul>
               @foreach(var item in subCat.Children()){
                <li>
                   <a href="@item.Url">@item.Name</a>
              </li>
               }
          </ul>
      }
    
  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Nov 18, 2018 @ 11:59
    Marc Goodson
    0

    Hi Matt

    Which template do you have the @foreach in?

    Eg do you intend to write out the products on your 'stock' page, underneath each category? or at this point has the user clicked on 'Lamps' and we're now on the StockSubCat page?

    (if we're executing the code in the context of the StockSubCat page, then you will get nothing, because Model.Content refers to the current page, eg Lamps, and your code only considers children of type StockSubCat, of which underneath lamps there are none...)

    However if you are just trying to write out the products on the Stock homepage, in the stock template, underneath each category, then what you have 'should work'

    regards

    marc

  • Matt 353 posts 825 karma points
    Nov 18, 2018 @ 13:38
    Matt
    0

    Hello Marc,

    Thanks for the reply.

    So the @foreach is in the StockSubCat page.

    By the @foreach point the user and clicked on Lamps (or chairs) and I'm trying to get all the lamps to show.

    But i want it more dynamic, so depending on which "StockSubCat" page your on it will display those products.

    Thanks

    Matt

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Nov 18, 2018 @ 14:42
    Marc Goodson
    100

    Hi Matt

    ok, so I sort of think I understand, if you change the foreach to be:

          <div>@Model.Content.Name</div>
          <ul>
    @foreach(var product in Model.Content.Children())
      {
                <li>
                   <a href="@product.Url">@product.Name</a>
              </li>
    
      }      </ul>
    

    does that give you your product listing?

    If I've understood correctly you are on the 'Lamps' page and using the StockSubCat template, and in this context Model.Content will refer to the 'Lamps' page, and so in your foreach, you just want to loop through the children of this page that will be the products...

    regards

    marc

  • Matt 353 posts 825 karma points
    Nov 19, 2018 @ 15:02
    Matt
    0

    Aha that worked a dream, thank you :)

Please Sign in or register to post replies

Write your reply to:

Draft