Copied to clipboard

Flag this post as spam?

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


  • Chris 9 posts 65 karma points
    Aug 19, 2014 @ 19:42
    Chris
    0

    NodeById - Umbraco doesn't like it

    Hello everyone,

    I am following a tutorial (posted here) which explains a way to create footer navigation. I'm setting a NaviHide Property within the pages I don't want to show, and also using 'umbracoRedirect' to push me to the pages I do. My Footer Nav macro also has a "Source" parameter that I am passing along as a Content Picker when inserting it in the template. The redirect portion is fine, but I am getting stymied when trying to complete the last portion, which is to create a macro which shows certain pages. Here is the macro in question (created as a Partial View Macro):

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    var parent = @Model.NodeById(Parameter.Source);
    if (parent != null) {
            <ul>
                @foreach (var item in parent.Children.Where("Visible")) {
                    <li>                   
                        <a href="@item.Url">@item.Name</a>                   
                    </li>
                }
            </ul>
        }

    However, when running the page, I get an unhelpful "Error Loading Partial View script (file: ~/Views/MacroPartials/FooterNav2.cshtml)" message.

    I've tried recreating it as a regular Partial View in the Settings Menu, which at least gives me a compilation error message. The error says "CS1061: 'Umbraco.Web.Models.PartialViewMacroModel' does not contain a definition for 'NodeById' and no extension method 'NodeById' accepting a first argument of type 'Umbraco.Web.Models.PartialViewMacroModel' could be found (are you missing a using directive or an assembly reference?)"

    The offending portion of my code is the "var parent = @Model.NodeById(Parameter.Source);" line. Is there something else I should be using besides "NodeById"? Is it even possible for me to grab a specific page and say I want all of its children so that I can display them as a menu?

    On a related note: I am continually frustrated by not knowing what properties, methods, and data I can access from a Partial View. Is there any sort of reference that shows these items? Is there anything I can add to my URLs to debug things? I'm swimming blind out here, even after having gone through the Implementor video tutorials.

    Thanks and bless ya...

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 19, 2014 @ 19:46
    Jeavon Leopold
    0

    Hi Chris,

    That tutorial is slightly out of date and is for the now legacy "Razor Macro" where as you are using the current "Partial View Macro". It is easily adapted though:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    var parent = Umbraco.Content(Model.MacroParameters["Source"].ToString());
    if (parent != null) {
            <ul>
                @foreach (var item in parent.Children.Where("Visible")) {
                    <li>                    
                        <a href="@item.Url">@item.Name</a>                    
                    </li>
                }
            </ul>
        }
    

    Jeavon

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 19, 2014 @ 19:48
    Jeavon Leopold
    100

    Sorry, I was missing the brackets:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var parent = Umbraco.Content(Model.MacroParameters["Source"].ToString());
        if (parent != null)
        {
            <ul>
                @foreach (var item in parent.Children.Where("Visible"))
                {
                    <li>
                        <a href="@item.Url">@item.Name</a>
                    </li>
                }
            </ul>
        }
    }
    
  • Chris 9 posts 65 karma points
    Aug 19, 2014 @ 21:20
    Chris
    0

    Thanks for the reply, Jeavon! That's helpful to know there's a new syntax. Is the UmbracoHelper page a good reference for finding methods to query data out of Umbraco, or is there a better page?

    I had to make one more adjustment to the code: My syntax for defining a variable was undefined. I had to wrap my variable in brackets with an @ symbol: @{ var x = something }.

    However, I'm still having an issue where my "if" statement is getting rendered as HTML rather than Razor code. Any idea why?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 19, 2014 @ 21:32
    Jeavon Leopold
    0

    Yes, the UmbracoHelper documentation and the general MVC documentation There are also a lot of helpful snippets for the property editors here

    Hopefully you have seen my second post above with the braces?

  • Chris 9 posts 65 karma points
    Aug 19, 2014 @ 21:39
    Chris
    0

    Ah, that fixes it - I didn't notice where you started the code with an "@{". Sorry about that!

    And thanks for the references, I'll make sure to bookmark them.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 19, 2014 @ 23:57
    Jeavon Leopold
    0

    Hi Chris,

    Great! could you please mark the correct solution above by using the green tick?

    Thanks,

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft