Copied to clipboard

Flag this post as spam?

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


  • Sjoerd Stottelaar 26 posts 181 karma points
    Mar 21, 2019 @ 11:12
    Sjoerd Stottelaar
    0

    Get value from other node with Umbraco helper

    Hi there,

    What is the best way to get values from other nodes in Umbraco? I want to use this to query general settings on my website. I have a dedicated node in my content tree where all settings are. I've tried using this:

    var settings = Umbraco.Content(2017)
    

    This returns an "Umbraco.Web.PublishedModels.Homepage" object as expected. When I want to pick some values from this model, it won't let me. For example, I want to do this:

    @settings.WebsiteName
    

    This returns an error stating "'IPublishedContent' does not contain a definition for 'WebsiteName'". It does work when I add the Value method:

    @settings.Value("websiteName")
    

    This works for a textbox field, but not for other properties like nested content or a link property.

    Does anyone know how to correctly query these properties as well? Thanks in advance!

  • Paul Wright (suedeapple) 260 posts 662 karma points
    Mar 21, 2019 @ 12:27
    Paul Wright (suedeapple)
    0

    Are you using Models Builder?

    A property of type "nestedcontent" will return a list of nodes, which you will need to iterate through.

    Read through the relevant Umbraco documention to see how each property is returned, and how best to handle.

  • Sjoerd Stottelaar 26 posts 181 karma points
    Mar 21, 2019 @ 12:37
    Sjoerd Stottelaar
    0

    Thanks for your answer. Yes, I think we are using the Models Builder, our configuration looks like this: enter image description here

    Though I can't use the Models Builder like I would in let's say a partial view (Model.WebsiteName e.g.). It returns an: "'IPublishedContent' does not contain a definition for 'WebsiteName'".

    I've both tried it on my master template and in a partial view, both result in these errors.

  • Paul Wright (suedeapple) 260 posts 662 karma points
    Mar 21, 2019 @ 12:43
    Paul Wright (suedeapple)
    0

    Try something like

    var settings = Umbraco.TypedContent(2017).OfType<HomePage>();
    

    I think the "content" method just returns a dynamic type.

    The "P" on HomePage might not be captilized in your project - Depends how you spelt "homepage" or "home page" :)

    You could do this as well, which might be more efficient, and doesnt rely on hardcoding the ID

        var settings = Model.Site().OfType<HomePage>();
    
Please Sign in or register to post replies

Write your reply to:

Draft