Copied to clipboard

Flag this post as spam?

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


  • Steve 472 posts 1216 karma points
    Aug 26, 2015 @ 13:19
    Steve
    0

    Problems with Children

    I am constructing a Razor script to paginate our NewsArticles, but when I try to access them using :

    var articles = Model.Parent.Descendants("DateFolder").Children.Where("NodeTypeAlias == \"NewsArticle\"");
    

    Where the structure looks like my photo attachment, the script fails to load. What's wrong with my syntax?enter image description here

  • Steve 472 posts 1216 karma points
    Aug 26, 2015 @ 14:12
    Steve
    0

    I get a casting error when I use your lambda.

  • Christopher Pascual 25 posts 273 karma points
    Aug 26, 2015 @ 14:17
    Christopher Pascual
    0

    sorry , I tried to delete my comment as fast as I could

  • Steve 472 posts 1216 karma points
    Aug 26, 2015 @ 14:18
    Steve
    0

    Here is the entire Razor:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{ 
        int pageSize; // How many items per page
        int page; // The page we are viewing
    
        /* Set up parameters */
    
        if (!int.TryParse(Parameter.PageSize,out pageSize))
        {
            pageSize = 6;
        }
    
        if (!int.TryParse(Request.QueryString["page"],out page))
        {
            page = 1;
        }
    
        /* This is your basic query to select the nodes you want */
    
        var articles = Model.Parent.Descendants("DateFolder").Children.Where("NodeTypeAlias == \"NewsArticle\"");
    
    
        int totalNodes = articles.Count();
        int totalPages = (int)Math.Ceiling((double)totalNodes /(double)pageSize);
    
        /* Bounds checking */
    
        if (page > totalPages)
        {
            page = totalPages;  
        }
        else if (page <1)
        {
            page = 1;
        }
    }
    
    <h2>Found @totalNodes results.Showing Page @page of @totalPages</h2>
    
    <ul>
        @foreach (var item in articles.Skip((page - 1) * pageSize).Take(pageSize))
        {
            <li><a href="@item.Url">@item.Name</a>(@item.DisplayDate.ToShortDateString())</li>
        }
    </ul>
    
    <ul class="paging">
        @for (int p = 1; p < totalPages + 1; p++)
        {
            string selected = (p == page) ? "selected" : String.Empty;
            <li class="@selected"><a href="?page=@p" title="Go to page @p of results">@p</a></li> 
        }
    </ul>
    
  • Christopher Pascual 25 posts 273 karma points
    Aug 26, 2015 @ 14:24
    Christopher Pascual
    0

    can you try this sir steve :

     var articles = Model.Parent.Descendants("NewsArticle");
    
  • Steve 472 posts 1216 karma points
    Aug 26, 2015 @ 14:28
    Steve
    0

    I have tried this, and it does list all the articles, but I need only the articles contained (children) in the folder of the docType "DateFolder".

    Otherwise, I get some unneeded articles that are outside the DateFolders.

  • Jerode 44 posts 159 karma points
    Aug 26, 2015 @ 15:30
    Jerode
    0

    Would you be willing to do something like:

    var articles = uQuery.GetNodeByUrl($"/Alumni/{DateFolder}/")
                    .GetChildNodes()
                    .Where(x => x.NodeTypeAlias == "NewsArticle")
    

    This will use uQuery and just get all the children at that URL of the NodeType.

  • Steve 472 posts 1216 karma points
    Aug 26, 2015 @ 16:23
    Steve
    0

    I guess I am okay with this for now, but could you look at my syntax for 2 things in the for loop that lists the NewsArticles:

    1. I am trying to get the "NewsCategory" Name from the current article, but it's not working. The NewsCategory is a Ancestor of the current node, so I thought I could reference it that way in the loop. Nope..

    2. I am also trying to get an image from the media library with the property of "featuredImage". The if statement works, but I only get the "Hello World" text output in the image's place.

    @foreach (var item in articles.OrderBy("CreateDate descending").Skip((page - 1) * pageSize).Take(pageSize)) {

    var newsCategory = @Model.Ancestor("NewsCategory");

            <li class="listing">
                <p class="tag">@newsCategory.Name - @item.CreateDate.ToShortDateString())</p>
                    @if( @item.HasValue("featuredImage")){
                        var featuredImageThumb = @Model.Media("featuredImage");
                    <img src="/[email protected]&width=154" />
                    }
                    <h3><a href="@item.Url">@item.headline</a></h3></li>
                    <p class="teaser">@item.teaser</p>
        }
    
  • Steve 472 posts 1216 karma points
    Aug 26, 2015 @ 17:03
    Steve
    0

    Going back, It does look like I'll have to be able to select the articles from the docType of "DateFolder" and I just can't seem to get that to work at all.

    I don't understand why this won't work to get the children of the children of another node using something like this:

    var articles = Model.AncestorOrSelf("NewsHome").Children.Where("NodeTypeAlias == \"NewsCategory\"").Children.Where("NodeTypeAlias == \"DateFolder\"");

    enter image description here

Please Sign in or register to post replies

Write your reply to:

Draft