Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Nov 21, 2014 @ 15:02
    Craig O'Mahony
    0

    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 

    @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.

    Could anybody help please?

    Thanks,

    Craig

  • Jeavon Leopold 3073 posts 13630 karma points MVP 11x admin c-trib
    Nov 21, 2014 @ 15:06
    Jeavon Leopold
    0

    As you are using the strongly typed model you cannot access custom properties dynamically so it should be like this

      @myPage.GetPropertyValue("homePageImageLink")
    

    Jeavon

  • Craig O'Mahony 364 posts 918 karma points
    Nov 21, 2014 @ 15:10
    Craig O'Mahony
    0

    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?

     

  • Jeavon Leopold 3073 posts 13630 karma points MVP 11x admin c-trib
    Nov 21, 2014 @ 15:16
    Jeavon Leopold
    101

    Are you totally sure that the property alias is correct and that it has a value on the nodes being iterated?

  • Craig O'Mahony 364 posts 918 karma points
    Nov 21, 2014 @ 15:28
    Craig O'Mahony
    0

    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!

     

  • Phill 115 posts 288 karma points
    Nov 21, 2014 @ 15:38
    Phill
    0

    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="" /> } }

  • Craig O'Mahony 364 posts 918 karma points
    Nov 21, 2014 @ 15:42
    Craig O'Mahony
    0

    Just got there!

    You, my friend, are an international superstar!

    Thanks loads!

  • Jeavon Leopold 3073 posts 13630 karma points MVP 11x admin c-trib
    Nov 21, 2014 @ 16:22
    Jeavon Leopold
    0

    Good job!

    My media item suggestion would be like this:

    @{
        if(myPage.HasValue("homePageImageLink")){
            var mediaItem = Umbraco.TypedMedia(myPage.GetPropertyValue("homePageImageLink")); 
            <img src="@mediaItem.Url" alt="@mediaItem.Name"/>    
        }   
    }
    
  • Phill 115 posts 288 karma points
    Nov 21, 2014 @ 16:25
    Phill
    0

    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

  • Jeavon Leopold 3073 posts 13630 karma points MVP 11x admin c-trib
    Nov 21, 2014 @ 17:17
    Jeavon Leopold
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft