I'm having trouble accessing a node custom properties I can access it 'normal' stuff ok. My code is
@foreach(var imagePage in Model.Content.Children.Where(c => c.DocumentTypeAlias.Equals("HomePageSideImagery"))) { foreach(var myPage in imagePage.Children) { <li><a href="@myPage.Url">@myPage.homePageImageLink</a></li> } }
@myPage.Url or @myPage.Name works fine but @myPage.homePageImageLink throws 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'homePageImageLink error.
I'm not an Umbraco expert so just using code I found a while back. Is it not possible in your code that the media item could be removed, without updating the page and it would throw an ugly YSOD error? That's why I check to make sure that the media item isn't null.
Also, what's the trick to getting code snippets to display properly on these forums? I went to html mode and wrapped in <code></code> but looks like crap.
Unable to access custom node properties
Hi Folks,
I'm having trouble accessing a node custom properties I can access it 'normal' stuff ok. My code is
@myPage.Url or @myPage.Name works fine but @myPage.homePageImageLink throws 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'homePageImageLink error.
Could anybody help please?
Thanks,
Craig
As you are using the strongly typed model you cannot access custom properties dynamically so it should be like this
Jeavon
HI Jeavon,
Thanks for that, using your suggestion doesn't return the value that's in the textstring (homePageImageLink), the page isn't erroring now though!
Any ideas?
Are you totally sure that the property alias is correct and that it has a value on the nodes being iterated?
Wow that was a bit weird! I created another test property and that worked (as you suggested) and then my original homePageImageLink started working!!
Now just to work out how to convert a media id (which is what homePageImageLink actually returns) and I'm flying!
Hi Craig, not sure if you're there yet but to save you a bit of time, here's a little code snippet I use for getting image from media id:
@{ var mediaItem = Umbraco.TypedMedia(p.GetPropertyValue("homePageImageLink")); if (mediaItem != null) { <img src="@mediaItem.GetPropertyValue("umbracoFile") alt="" /> } }
Just got there!
You, my friend, are an international superstar!
Thanks loads!
Good job!
My media item suggestion would be like this:
Hi Jeavon,
I'm not an Umbraco expert so just using code I found a while back. Is it not possible in your code that the media item could be removed, without updating the page and it would throw an ugly YSOD error? That's why I check to make sure that the media item isn't null.
Also, what's the trick to getting code snippets to display properly on these forums? I went to html mode and wrapped in <code></code> but looks like crap.
Phill
Hi Phil,
You are right, a null check is a good idea as it handles the media item having being deleted.
I would recommend using the markdown editor on the forum to mark you code snippet.
Regards,
Jeavon
is working on a reply...