Computer Magic Logo
Up, Down, Next and Previous

Saturday, August 8, 2015

Published by Aristotelis Pitaridis

The DynamicNodeWalker class allows us to use four methods on a DynamicNode in order to move to the up, down, next and previous DynamicNode. Let’s see some examples in order to understand how to use these methods. We suppose that we have the following page structure.

Now we assume that we are in the path "/Page 2/Page 2, 1/Page 2, 1, 2" and we type the following code in our view.

@CurrentPage.Up().Name

We use the Up() member function to go up one level in the pages structure and this will have as a result to get the parent page of the current page. This is not something new. We have done it with the Parent property but the good thing is that we can combine it with the other three member functions to reach every page in our site in case that we know the structure of our site. The Up() member function can take a number as parameter. For example if we use the number 1 we ask not just one level up in the structure but we ask to add one extra level. This means that we will get up two levels in the page structure.

@CurrentPage.Up(1).Name

This line of code will display the name of the Page-2 page. Let’s see something more complex.

@CurrentPage.Up().Down().Name

Now we use the Up() member function which returns the "Page 2, 2" and after that we use the Down() member function which will have as a result to get to the first node of its children list. This means that we will get the "Page 2, 2, 1" page.

Let’s see how to use the Previous() member function which return the previous page of the same level. We type the following command.

@CurrentPage.Up().Previous().Down().Name

This command will go up one level and it will return the page "Page 2, 2" and after that we use the Previous() member function and it will return the page "Page 2, 1" and after that we use the Down() member function which will return the first child page which is the page "Page 2, 1, 1". 

The final member function to use is the Next(). We will attach it to the previous example in order to get the second child of the "Page 2, 1" page.

@CurrentPage.Up().Previous().Down().Next().Name

With this four member functions we can move around and have access to every possible page of our site. In case that we follow a path which does not contain any page the member functions will return a null value.

@{
    var MyNode = CurrentPage.Down();
}

@if (MyNode == null)
{
    <p>The Down() member function returned null.</p>
}
else
{
    <p>@MyNode.Name</p>
}

This means that if we are not 100% sure that the node we are looking for exists, we h have to write the equivalent code which will make all the required checks so that our application will not display error message.