Copied to clipboard

Flag this post as spam?

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


  • Marrix 24 posts 156 karma points
    Jul 06, 2017 @ 08:22
    Marrix
    0

    Get a list of media/pdf (multiple media picker) from partial view

    I have this structure:

    Home
    --> Productoverview
    ----> Category
    ------> ProductItem (has a multiple media picker for uploading pdf-documents)

    And now - in a partial view - I need a list of all these productItem-pdf across categories. How? I have tried this below (and a lot of stupid variants) - the first for-each works fine, but the next (ouch!) ... and perpaps there is an even more simple or elegant way to fix it?

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        var selection = Model.Content.Site().FirstChild("productOverview").Descendants("productItem");
    }
    
    <ul>
        @foreach (var item in selection)
        {
            <li>
                <a href="@item.Url">@item.Name ...</a>
                @foreach (var item2 in item.???Getpropertyvalues("productPdf").something)
                {
                    //Name and link of each pdf from all products across categories
                }
            </li>
        }
    </ul>
    
  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jul 06, 2017 @ 08:57
    Alex Skrypnyk
    101

    Hi Marrix

    Try this code:

    @{
        var selection = Model.Content.Site().FirstChild("productOverview").Descendants("productItem");
    }
    
    <ul>
        @foreach (var item in selection)
        {
            <li>
                <a href="@item.Url">@item.Name ...</a>
                @foreach (var item2 in item.GetPropertyValue<IEnumerable<IPublishedContent>>("productPdf"))
                {
                    //Name and link of each pdf from all products across categories
                    <a href="@item2.Url">@item2.Name</a>
                }
            </li>
        }
    </ul>
    
  • Marrix 24 posts 156 karma points
    Jul 06, 2017 @ 09:15
    Marrix
    1

    Hi Alex

    ... my very best Umbraco-friend ^_^ Thanks again - it works!

    A small addition - if-clause - because not all productItem has a pdf ... maybe that can be done better/smarter?

    <ul>
        @foreach (var item in selection)
        {
            <li>
                <a href="@item.Url">@item.Name ...</a>
                @if (item.GetPropertyValue<IEnumerable<IPublishedContent>>("productPdf") != null)
                {
                    foreach (var item2 in item.GetPropertyValue<IEnumerable<IPublishedContent>>("productPdf"))
                    {
                        //Name and link of each pdf from all products across categories
                        <a href="@item2.Url">@item2.Name</a>
                    }
                }
    
            </li>
        }
    </ul>
    
  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jul 06, 2017 @ 09:24
    Alex Skrypnyk
    1

    You are welcome, glad to help!

    First of all, recommend you to read - https://our.umbraco.org/documentation/reference/Common-Pitfalls/

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jul 06, 2017 @ 09:32
    Alex Skrypnyk
    1

    Select only nodes with value:

    var selection = Model.Content.Site().FirstChild("productOverview").Descendants("productItem").Where(x => x.HasValue("productPdf"));
    

    And next improvement will be done not use "Descendants", it's much better to replace it with xPath.

    Alex

  • Marrix 24 posts 156 karma points
    Jul 06, 2017 @ 09:41
    Marrix
    0

    Okay. Thanks for help and tips and reading.
    xPath ... I will try. Have to read about that I guess. Thank you :-)

  • Marrix 24 posts 156 karma points
    Jul 06, 2017 @ 10:05
    Marrix
    0

    I have read about the Descendant-problem. But how can I avoid using it in my query?

    This will not work:

    var selection = Model.Content.Site().FirstChild("productOverview").Children("productItem").Where(x => x.HasValue("productPdf"));
    

    ... because productItem is not a direct child to productOverview - its a child-child ... descendant. I have tried this - the princip is correct but the syntax is not:

    var selection = Model.Content.Site().FirstChild("productOverview").Children("category").Children("productItem").Where(x => x.HasValue("productSheets"));
    

    Is there then a way to avoid using descendant in this?

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jul 06, 2017 @ 11:45
    Alex Skrypnyk
    1

    A way to avoid using descendant in this is xPath :)

  • Marrix 24 posts 156 karma points
    Jul 06, 2017 @ 13:52
    Marrix
    0

    Got it. Thanks. xPath ... sounds a little like old days and xml :-)

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jul 06, 2017 @ 14:16
    Alex Skrypnyk
    1

    Marrix, it's not the old way at all, as Umbraco uses xml structured cache for fron end rendering.

    Umbraco 8 will not use XML, and when it will release, it will be old way :)

  • Marrix 24 posts 156 karma points
    Jul 06, 2017 @ 15:19
    Marrix
    0

    HA! I'm ahead of Umbracos time :-D Meanwhile I'll take a look at xPath. Thanks again.

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jul 06, 2017 @ 15:27
    Alex Skrypnyk
    1

    You are welcome, Marrix, have a great evening!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies