Copied to clipboard

Flag this post as spam?

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


  • blackhawk 313 posts 1368 karma points
    Feb 13, 2018 @ 19:02
    blackhawk
    0

    Get a property on a specific content node using Model

    I'm on Umbraco 7.7.8

    I have the Meganav on a specific content node, and would like to reference it onto a template so that other content nodes use it. What would be the easiest way to reference it via model in razor?

    The property alias for the meganav is TopMenu and the content node id that dresses it up is 1064

    The following seems to make sense if I want to reference the property from the root location...

     var mmeganav = Model.Content.Site().GetPropertyValue<IEnumerable<MeganavItem>>("topMenu");
    

    But how would I modify this?

    Thanks

  • blackhawk 313 posts 1368 karma points
    Feb 13, 2018 @ 19:28
    blackhawk
    101

    I got it...

     var mmeganav = Umbraco.TypedContent(1064).GetPropertyValue<IEnumerable<MeganavItem>>("topMenu");
     if (mmeganav != null) {
     //do stuff...
     }
    

    Works like a charm

  • Streety 358 posts 568 karma points
    Feb 13, 2018 @ 20:51
    Streety
    1

    Sure you can do that but tying you code to a specific nodeID is a bad idea.

    A better method using Razor would be to traverse the tree to look for your mega nav type alias. This way you can change the node of the mega nav but as long as you type alias remains the same it will return a reliable collection of nodes. It also makes your code portable.

    var megaNav = Umbraco.TypedContentSingleAtXPath("//megaNavTypeAlias");
    
  • blackhawk 313 posts 1368 karma points
    Feb 14, 2018 @ 04:04
    blackhawk
    0

    Thanks for the good tip! I have not explored this yet. But to help me understand, does this process look for the first match of a property alias within the content tree? does it look through the whole content tree or just at root level?

    The reason I am asking is because my Meganav property alias is not sitting on the root of my content tree, it's one level deep in my content tree.

    Many Thanks!

  • Streety 358 posts 568 karma points
    Feb 14, 2018 @ 10:42
    Streety
    1

    It will traverse your tree looking for a type alias and return the first one it finds.

    With navigation, you may typically have 2-3 nav structures (unless you are evaluating your tree as your nav). Lets say, Mega Nav, Subnav and may be a separate Mobile Nav.

    Give these different type alias (just make a child node from your nav node (in doc types) and inherit all the properties). That way you really can't go wrong. Even if you move or copy the nav nodes the alias will remain bound.

  • blackhawk 313 posts 1368 karma points
    Feb 14, 2018 @ 16:02
    blackhawk
    0

    I get the concept, and read the docs on this, but still stuck on using it. On my view I have the following code...

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Cogworks.Meganav.Models
    @{
        Layout = "Master.cshtml";
    }
    
    ...
    
         var mmeganav = Umbraco.TypedContentSingleAtXPath("//topMenu");
         if (mmeganav != null)
         {
          <ul>
              @foreach (var item in mmeganav.children)
               {
               <li>
               @item.Name
               </li>
         }
        </ul>
        }
    

    This code returns the following error...

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 
    

    with the following highlighted in red:

    var mmeganav = Umbraco.TypedContentSingleAtXPath("//topMenu");
    

    Yet...this works....

      var mmeganav = Model.Content.GetPropertyValue<IEnumerable<MeganavItem>>("topMenu");
                        if (mmeganav != null)
                        {
                            <ul>
                                @foreach (var item in mmeganav)
                                {
                                    <li>
                                        <a href="@item.Url" target="@item.Target">@item.Title </a>
                                    </li>
                                }
                            </ul>
                        }
    

    What am I missing from my XPath logic?

  • Streety 358 posts 568 karma points
    Feb 15, 2018 @ 10:54
    Streety
    0

    OK i'll give you an example:

    This is a snippet from a Profile Helper class, which returns property values from document type called "siteLanguage". This runs from anywhere in the site.

            public static DefaultPageViewModel GetDefaultPages()
        {
            DefaultPageViewModel defaultPage = new DefaultPageViewModel();
    
            var lang = umbracoHelper.TypedContentSingleAtXPath("//siteLanguage");
    
            defaultPage.DeniedPage = lang.HasProperty("pageDenied") && lang.GetPropertyValue("pageDenied") != null ? umbracoHelper.Content(lang.GetPropertyValue("pageDenied")) : null;
            defaultPage.TermsPage = lang.HasProperty("terms") && lang.GetPropertyValue("terms") != null ? umbracoHelper.Content(lang.GetPropertyValue("terms")) : null;
            defaultPage.HomePage = lang.HasProperty("defaultHome") && lang.GetPropertyValue("defaultHome") != null ? umbracoHelper.Content(lang.GetPropertyValue("defaultHome")) : null;
    
            return defaultPage;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft