Copied to clipboard

Flag this post as spam?

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


  • Joshua Walsh 30 posts 151 karma points
    Jul 15, 2014 @ 07:32
    Joshua Walsh
    0

    Issues with traversing

    Hi everyone,

    I'm very new to Razor so please excuse my stupidity here. I've inherited a decently sized website that uses Umbraco and Razor. So far I'm loving Umbraco but HATING Razor.

    We've got a content tree that looks something like this:

    Root
    |
    |-Products
    | |
    | |-Product A
    | |
    | |-Product B
    |
    |-Solutions
    | |
    | |-Solution A
    | |
    | |-Solution B
    | |
    | |-Solution C
    | |
    | |-Roles
    |   |
    |   |-Role A
    |   |
    |   |-Role B
    | 
    |
    etc...
    

    This doesn't seem like the most efficient structure to me, but for now I am going to put up with it.

    We have a dropdown menu which currently allows the user to select from either the solutions or the roles. (The roles use the same Content Type as the solutions)

    <select id="Solutions" name="Solutions" style="width:225px"  >
        <option value="-1" >Select a solution</option>
        @foreach (var page in Model.AncestorOrSelf(1).Descendants("paSolution").Where("Visible"))
        {
            <option value="@page.Id" >@page.Name</option>                                     
        }
    </select>
    

    As a small part of a fairly large update, I have been asked to make this only show Solutions and not Roles. 'No worries' I thought, but roughly 3 hours later I'm starting to realise that there are actually some worries.

    I figure that once I can get hold of the Solutions node, it'll (hopefully) be easy to just grab .Children and iterate over those. The trick is that I can't actually get to the Solutions node.

    I tried doing this:

    var rootNodes = Model.AncestorOrSelf(1);
    var solutionsNode = rootNodes.Where(item => item.NodeTypeAlias == "paContent" && item.UrlName.ToLower() == "solutions");
    

    and got the following:

    Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

    So after a bit of Google I revised it to this:

    umbraco.MacroEngines.DynamicNode rootNodes = Model.AncestorOrSelf(1);
    var solutionsNode = rootNodes.Where(item => item.NodeTypeAlias == "paContent" && item.UrlName.ToLower() == "solutions");
    

    and now I get the following error:

    Cannot convert lambda expression to type 'string' because it is not a delegate type

    So I think now's probably a good time to admit that I'm out of my depth in Razor/C# and need some help.

    I really don't know what to do next. If it helps, I'm using Umbraco v4.7.1.1.

    Thanks,

    Josh

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 15, 2014 @ 08:04
    Dennis Aaen
    0

    Hi Josh and welcome to our.

    Maybe you could do something like this to get it work.

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    var rootNodes = Model.AncestorOrSelf(1);
    var solutionsNode = rootNodes.Where("NodeTypeAlias == @0 && UrlName.ToLower() == @1","paContent","solutions");
    }
    

    Take a look at the documentation for Filtering, Ordering & Extensions http://our.umbraco.org/documentation/Reference/Querying/DynamicNode/Collections#FilteringOrdering&Extensions as you can see you can do more detailed queries and conditions.

    Hope this helps,

    /Dennis

  • Joshua Walsh 30 posts 151 karma points
    Jul 15, 2014 @ 08:08
    Joshua Walsh
    0

    Thanks for your reply, Dennis. I'm now getting a new error:

    Can only unbox from an object or interface type to a value type.

    I'll have a bit of a play around.

    Cheers!

    EDIT: I'm silly. I removed ChildrenAsList from my code a while back and forgot to replace it with Children. My problem is now solved, thankyou very much!

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 15, 2014 @ 09:21
    Dennis Aaen
    1

    Hi Joshua,

    Great to hear that you solved your issue.

    You can mark this question as solved by clicking on the little green tick you have over each post. You should click the post that give you hint to solve your problem or the actual solution.

    But great you got your issue solved.

    Happy Umbraco coding, and use the forum if you have any problems or questions, people are friendly in here :-)

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft