Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    May 07, 2012 @ 14:50
    Anthony Candaele
    0

    problem with acces node with Razor

    Hi,

    To publish the latest publications on a homepage, I'm writing a Razor script.

    My Content Tree looks like this:

    A publication is stored in a yearfolder with the alias 'PublicationFolderYear'

    To get all publications of the current year, I'm using this Razor code:

    var year DateTime.Now.Year;
      var yearfolder @Model.AncestorOrSelf("PublicationArea").Children.Where("@Name == \"" year "\"");

    However, when debugging this code, the line:

    var yearfolder @Model.AncestorOrSelf("PublicationArea").Children.Where("@Name == \"" year "\"");

    produces this error:

    'cannot perform runtime binding on a null reference'

    Does someone know what I'm doing wrong?

    Thanks for your help,

    Anthony

  • gilad 185 posts 425 karma points
    May 07, 2012 @ 14:54
    gilad
    1

    Hii Anthony.

    I am not sure if this is your problem but try this : 

    var yearfolder @Model.AncestorOrSelf("PublicationArea").Children.Where("Name == \"" year "\"");

    (without the @ before Name)

    Hope that help, Cheers.

  • Anthony Candaele 1197 posts 2049 karma points
    May 07, 2012 @ 15:00
    Anthony Candaele
    0

    Hi gilad,

    I already tried that but with no succes, I also tried this:

    var yearfolder @Model.AncestorOrSelf("PublicationArea").Children.Where("NodeName == \"" year "\"");

    also without succes :(

    Thanks for your help anyway,
    Anthony

  • gilad 185 posts 425 karma points
    May 07, 2012 @ 15:04
    gilad
    0

    Maybe try this : 

    var yearfolder @Model.AncestorOrSelf("PublicationArea").Descendants("PublicationFolderYear").Where("NodeName == \"" year "\"");

     


  • Anthony Candaele 1197 posts 2049 karma points
    May 07, 2012 @ 15:16
    Anthony Candaele
    0

    Hi Gilad,

    I found the solution, this works :) :

    var yearfolder @Model.Up.Descendants("PublicationArea").Children("PublicationFolderYear").Where("nodeName == \"" year "\"");

    Thanks for your help,
    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    May 07, 2012 @ 15:21
    Anthony Candaele
    0

    Alas, seems I was a bit too euphoric, the code still does not work, when I debug the code, my yearfolder variable is emty (the message in the debugger is 'Enumeration yielded no results')

    :(

  • gilad 185 posts 425 karma points
    May 07, 2012 @ 15:25
    gilad
    0

    try this : 

    var yearfolder @Model.AncestorOrSelf("PublicationArea").Descendants("PublicationFolderYear").Where("Name == \"" year "\"").First();

    Can you paste the hole code block?

  • Anthony Candaele 1197 posts 2049 karma points
    May 07, 2012 @ 15:35
    Anthony Candaele
    0

    Hi gilad,

    that didn't work. Apparently if I use @Model.AncestorOrSelf... I get an 'cannot perform dynamic binding on a null reference'

    If I use @Model.Up.Descendants("PublicationArea")... I don't get an error, but the enumeration didn't yield a result.

    I don't have that much code to show:

    @{

      var year = DateTime.Now.Year;

      var yearfolder = @Model.AncestorOrSelf("PublicationArea").Descendants("PublicationFolderYear").Where("NodeName == \"" + year + "\"").First();

      DynamicNodeList publications = new DynamicNodeList();

      }

    I just wanted to test if this code returns an instance of PublicationFolderYear with a nodeName value of '2012'

    Anthony

  • gilad 185 posts 425 karma points
    May 07, 2012 @ 15:53
    gilad
    1

    Hi Anthony.

    just now see that you try to get it from home-page.

    are you sure about the - PublicationArea alias?

    try this for check if get something.

     @Model.AncestorOrSelf().Descendants("PublicationArea").Count

    if you get 1 - is good.

    but you can also try to go straight  to PublicationFolderYear after AncestorOrSelf() - without any parameter it is take the highest node.

    try this : 

    var yearfolder @Model.AncestorOrSelf().Descendants("PublicationFolderYear").Where("Name == \"" year "\"").First();

  • Anthony Candaele 1197 posts 2049 karma points
    May 07, 2012 @ 16:10
    Anthony Candaele
    0

    Hi gilad,

    I tested with 

    @Model.AncestorOrSelf().Descendants("PublicationArea").Count

    that gave me a value like 0x00000000001 or something

    Than I tried your code:

    var yearfolder @Model.AncestorOrSelf().Descendants("PublicationFolderYear").Where("Name == \"" year "\"").First();


    This works!

    I thought I had to work my way down through the hierarchy but apparently I can go straight to the 'PublicationFolderYear' node.

    thanks a lot, you rock!

    Anthony

Please Sign in or register to post replies

Write your reply to:

Draft