Copied to clipboard

Flag this post as spam?

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


  • Greg Berlin 818 posts 634 karma points
    Mar 25, 2012 @ 09:21
    Greg Berlin
    0

    Simple Razor Question

    From a Razor newbie... Why does this function call work:

    @RenderChildren(node.Children.Where("NodeTypeAlias == @0 or NodeTypeAlias == @1 and Visible", "FP_Page", "FP_Home"));

    whereas this does not:

    dynamic children = node.Children.Where("NodeTypeAlias == @0 or NodeTypeAlias == @1 and Visible", "FP_Page", "FP_Home");                 
    RenderChildren(children); 

    Am i doing something wrong?  Declaring the variables wrong perhaps?

    I dont get an error or anything, it just doesnt call the function in the second case... the entire code block & helper function looks like this:

    @helper RenderNodes(dynamic nodes)
    {
        foreach (var node in nodes)
        {
            var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"current\"" : "";
    
            <li>
                <a @Html.Raw(selected) href="@node.Url" title="@node.Name" >@node.Name</a>
                 @RenderChildren(node.Children.Where("NodeTypeAlias == @0 or NodeTypeAlias == @1 and Visible", "FP_Page", "FP_Home"));
    @*            @{
                    dynamic children = node.Children.Where("NodeTypeAlias == @0 or NodeTypeAlias == @1 and Visible", "FP_Page", "FP_Home");
                    RenderChildren(children);
                }
    *@        </li>
        }
    }
    
    @helper RenderChildren(dynamic childNodes)
    {
        <p>children: @childNodes.Items.Count</p>
            <ul>
                @foreach (dynamic node in childNodes)
                {
                    var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"current\"" : "";
                    <li><a @Html.Raw(selected) href="@node.Url" title="@node.Name" >@node.Name</a></li>
                }
            </ul>  
    }
    
    
  • Greg Berlin 818 posts 634 karma points
    Mar 25, 2012 @ 21:06
    Greg Berlin
    0

    I've worked around the above issue by putting a condition around the code block in the RenderChildren helper.. but I'd still love to know why i can't get that other call to work...

    @helper RenderNodes(dynamic nodes)
    {
        foreach (var node in nodes)
        {
            var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"current\"" : "";
    
            <li>
                <a @Html.Raw(selected) href="@node.Url" title="@node.Name" >@node.Name</a>
                 @RenderChildren(node.Children.Where("NodeTypeAlias == @0 or NodeTypeAlias == @1 and Visible", "FP_Page", "FP_Home"))
            </li>
        }
    }
    
    @helper RenderChildren(dynamic childNodes)
        {
        if (@childNodes.Items.Count > 0) {
            <ul>
                @foreach (dynamic node in childNodes)
                {
                    var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"current\"" : "";
                    <li><a @Html.Raw(selected) href="@node.Url" title="@node.Name" >@node.Name</a></li>
                }
            </ul>  
            }
    }
    

     

Please Sign in or register to post replies

Write your reply to:

Draft