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>
@{
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>
... 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>
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?
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?
Hi Marrix
Try this code:
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?
You are welcome, glad to help!
First of all, recommend you to read - https://our.umbraco.org/documentation/reference/Common-Pitfalls/
Select only nodes with value:
And next improvement will be done not use "Descendants", it's much better to replace it with xPath.
Alex
Okay. Thanks for help and tips and reading.
xPath ... I will try. Have to read about that I guess. Thank you :-)
I have read about the Descendant-problem. But how can I avoid using it in my query?
This will not work:
... 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:
Is there then a way to avoid using descendant in this?
A way to avoid using descendant in this is xPath :)
Got it. Thanks. xPath ... sounds a little like old days and xml :-)
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 :)
HA! I'm ahead of Umbracos time :-D Meanwhile I'll take a look at xPath. Thanks again.
You are welcome, Marrix, have a great evening!
is working on a reply...