Copied to clipboard

Flag this post as spam?

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


  • Blake Watt (Clerke) 106 posts 351 karma points MVP 2x
    Feb 25, 2014 @ 03:41
    Blake Watt (Clerke)
    0

    Get next article with date > model.date

    I'm stuck and I appreciate any and all help I can get. I'm using Umbraco v4.11.10.

    I'm using date folders for a blog so my Content tree structure looks like this:

    Home
    - 2013
    - - 07
    - - - Article
    - 2014
    - - 02
    - - - Article

    When I'm on the Article page all I want to do is have a button that has a "Newer Post" and "Older Post" link but I can't get this to work and I've tried a numerous amount of ways/guesses. This is one of the ways I was trying to get the newer post Url.

    FYI: date is the alias of a datepicker on my Article node

    var newer = Model.AncestorOrSelf(1).Descendants().Where("NodeTypeAlias == \"Article\" && date > Model.date").OrderBy("date desc").First();

    With this code I am getting the following error message:

    Error Loading Razor Script (file: Article Pager) The binary operator GreaterThan is not defined for the types 'System.Object' and 'System.Object'.    at System.Linq.Expressions.Expression.GetUserDefinedBinaryOperatorOrThrow(ExpressionType binaryType, String name, Expression left, Expression right, Boolean liftToNull)

    The error is quite extensive, telling me I have a problem at line 4 in my cshtml file which is where I've declared the newer variable. 

    I've also tried mixing up the .Where() to include the Article NodeTypeAlias and CreateDate > Model.CreateDate as well as CreateDate > \"" + Model.CreateDate + "\" but I've had no luck. I'd really like to filter by the date if at all possible (vs using CreateDate).

    Thanks for any help! - Blake

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 25, 2014 @ 08:22
    Dennis Aaen
    100

    Hi Blake,

    You could try something like this and see if it does any difference.

    var newer =Model.AncestorOrSelf(1).Descendants().Where("NodeTypeAlias == @0 && date > @1", "Article",Model.date).OrderBy("date desc").First();

    You could try this one. I found inspiration here:

    var date = DateTime.Now.ToString("ddMMyyyy");
    var currentPageDate = Model.Date.ToString("ddMMyyyy");
    var
    newer = Model.AncestorOrSelf(1).Descendants("Article").Where("date > @1",currentPageDate).OrderBy("date desc").First();

    http://our.umbraco.org/forum/developers/razor/26022-Razor,-Where-%28DatetTime-Greater-Than-Or-Equal-DateX-%29

    Hope this help you further.

    /Dennis

  • Blake Watt (Clerke) 106 posts 351 karma points MVP 2x
    Feb 25, 2014 @ 18:29
    Blake Watt (Clerke)
    0

    You rock!! Thank you so much for your help & the other forum topic thread. I'm curious why the way I first wrote it wasn't working. Below ended up being my final code. I elimated the .First() because I was getting an error since there weren't any older posts. I used the .Count() to check if I needed my buttons. I really appreciate the help! I was stuck on this one. Thank you!

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
        var newer = Model.AncestorOrSelf(1).Descendants().Where("NodeTypeAlias == @0 && date > @1", "Article",Model.date).OrderBy("date desc");
        var older = Model.AncestorOrSelf(1).Descendants().Where("NodeTypeAlias == @0 && date < @1", "Article",Model.date).OrderBy("date desc");  
    }
    
    <div class="paginate">
        <div class="container">
            <div class="row">
                <div class="col-xs-6">
                    @if(older.Count() > 0)
                    {
                        <a class="btn btn-primary btn-lg" href="@older.First().Url">&laquo; Previous</a>                      
                    }
                </div>
                <div class="col-xs-6 text-right">
                    @if(newer.Count() > 0)
                    {
                        <a class="btn btn-primary btn-lg" href="@newer.First().Url">Next &raquo;</a>
                    }
                </div>
            </div>            
        </div>        
    </div>
Please Sign in or register to post replies

Write your reply to:

Draft