Looping through multinode treepicker and media picker and creating images
Hi everyone. I'm having an issue with looping through images on a page and getting it to display all the images. I have 4 images in a media picker, in a multinode treepicker. I think my loop may be bad? maybe I'm calling something incorrectly? Whichever it is, I am not sure.
@{
foreach (var temp in Umbraco.Content(clientCollection))
Where clientCollection is var clientCollection = clientList; and clientList is var clientList = CurrentPage.clientList.ToString().Split(new string[] { "," },
In Umbraco, if i remove all but one of the items in clientList, so I only have one node of the multinode treepicker, it works just fine and prints out exactly how I want it to. As soon as I add the other clients, I get this error thrown at the start of that loop. Any suggestions?
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The call is ambiguous between the following methods or properties: 'Umbraco.Web.UmbracoHelper.Media(params int[])' and 'Umbraco.Web.UmbracoHelper.Media(params string[])'
It's much happier, and getting passed the temp now, but the same issue remains on the <img tag. As a test, I swapped out the temp.clientLogo with id.clientLogo, just to see what it would print out, and it printed that the string does not contain a method called clientLogo, which while makes sense, is a little different.
Hm... Tested the temp.GetPropertValue<int>("clientLogo") And I got
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Umbraco.Web.Models.PublishedContentBase' does not contain a definition for 'GetPropertValue'
eI made sure to set the var as TypedContent as well. When swapping out the temp.clientLogo tags for the temp.GetPropertyValue... in the image tag, I get the same error. When I leave it as it was, as temp.clientLogo, with the TypedContent in place, I get
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Umbraco.Web.Models.PublishedContentBase' does not contain a definition for 'clientLogo'
Thanks, Yea usually i would have a model being passed in to the view / partial with the media object already instanced or properties with URL ect ect :)
Looping through multinode treepicker and media picker and creating images
Hi everyone.
I'm having an issue with looping through images on a page and getting it to display all the images. I have 4 images in a media picker, in a multinode treepicker. I think my loop may be bad? maybe I'm calling something incorrectly? Whichever it is, I am not sure.
@{
foreach (var temp in Umbraco.Content(clientCollection))
{
<img src="@Umbraco.Media(temp.clientLogo).Url" alt="@Umbraco.Media(temp.clientLogo).UrlName" />
}
}
Where clientCollection is var clientCollection = clientList; and clientList is
var clientList = CurrentPage.clientList.ToString().Split(new string[] { "," },
In Umbraco, if i remove all but one of the items in clientList, so I only have one node of the multinode treepicker, it works just fine and prints out exactly how I want it to. As soon as I add the other clients, I get this error thrown at the start of that loop. Any suggestions?
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The call is ambiguous between the following methods or properties: 'Umbraco.Web.UmbracoHelper.Media(params int[])' and 'Umbraco.Web.UmbracoHelper.Media(params string[])'
Try this code :
clientCollection is a list of id's. And Umbraco.Content only can take one id.
Dave
Ah, I understand. I'm still getting the same issue, but I was thinking it might have something to do with that.
The error is now on the var temp = Umbraco.Content(id); if that helps.
Is it the same error or something different ?
Same one
Can you try to out put just the id's to see what that gives you :
Dave
Man, this just confuses me even more. I was hoping it wasn't working, but when I tried that, I get all the id's perfectly.
1122
1130
1134
1135
Ah..I found it. Umbraco.Content takes a int parameter if I am correct. This "should" work.
Dave
It's much happier, and getting passed the temp now, but the same issue remains on the <img tag.
As a test, I swapped out the temp.clientLogo with id.clientLogo, just to see what it would print out, and it printed that the string does not contain a method called clientLogo, which while makes sense, is a little different.
Try this :
var temp = Umbraco.TypedContent(int.Parse(id));
And then you should try temp.GetPropertValue<int>("clientLogo").
This way you use the strongly typed IPublishedContent model instead of the dynamic model which is hard to debug.
Dave
Hm... Tested the temp.GetPropertValue<int>("clientLogo") And I got
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Umbraco.Web.Models.PublishedContentBase' does not contain a definition for 'GetPropertValue'
eI made sure to set the var as TypedContent as well. When swapping out the temp.clientLogo tags for the temp.GetPropertyValue... in the image tag, I get the same error. When I leave it as it was, as temp.clientLogo, with the TypedContent in place, I get
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Umbraco.Web.Models.PublishedContentBase' does not contain a definition for 'clientLogo'
I'm not sure what's going wrong at this point.
Can you post the entire code of your view ?
Dave
As of now, the view is incredibly long, so I removed most of the </divs> and such,
@inherits UmbracoTemplatePage
@using umbraco.MacroEngines
@{
Layout = "Base.cshtml";
var homePage = CurrentPage.AncestorsOrSelf(1).First();
var clientList = CurrentPage.clientList.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
var clientCollection = clientList;
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery.bxslider.min.js"></script>
<link href="/css/jquery.bxslider.css" rel="stylesheet" />
}
(useless working divs here, no code at all)
<div class="small-12 large-10 columns logos">
<ul class="small-block-grid-2 medium-block-grid-4 large-block-grid-4">
@{
foreach (var id in clientCollection)
{
var temp = Umbraco.TypedContent(int.Parse(id));
if(temp != null)
{
<img src="@Umbraco.Media(temp.clientLogo).Url" alt="@Umbraco.Media(temp.clientLogo).UrlName" />
}
}
}
</ul>
</div>
Still stuck on this =\ Does anyone have any ideas?
Sure what is the problem with it :)
So if you have a property with media ids
You can use the MediaService to get a object of type IMedia
MediaService mediaService = ApplicationContext.Current.Services.MediaService; var media = mediaService.GetMediaById(your id)
if(media != null) { var url = media.GetValue("umbracoFile"); }
I have not got visual studio open for intellisense but it should be something like that. :).
Let us know if that helps :).
Charlie
Hi Charles,
The media service is not for use in you views as this queries the database and can become a performance bottle neck.
Dave
Hi Brian,
This code works for me assuming that the clientList property is a mediapicker.
Dave
Awesome thanks for that Dave.
Re: Media Service.
Thanks, Yea usually i would have a model being passed in to the view / partial with the media object already instanced or properties with URL ect ect :)
What is the performance with Umbraco.TypedMedia?
Charlie
is working on a reply...