Copied to clipboard

Flag this post as spam?

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


  • Jebastin 42 posts 91 karma points
    Sep 24, 2013 @ 11:49
    Jebastin
    0

    How to find the Next & Previous Node ID by Current Node ID using Razor?

    I need some help in finding the Next & Previous Node ID by Current Node ID using Razor.

  • Jebastin 42 posts 91 karma points
    Sep 24, 2013 @ 12:02
    Jebastin
    1

    I found the solution myself. I've given the solution here; it may help to someone.

    dynamic CurrentNode = new DynamicNode().NodeById(CurrentNodeId);
    var PreviousNodeId = CurrentNode.Previous().Id;
    var NextNodeId = CurrentNode.Next().Id;
  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Sep 24, 2013 @ 12:09
    Jeroen Breuer
    0

    Hello,

    Did you try something like this?

    @Model.Content.Next()

    Have a look at the Razor cheat sheets:

    http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

    http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet

    Jeroen

  • Fuji Kusaka 2203 posts 4220 karma points
    Sep 24, 2013 @ 12:16
    Fuji Kusaka
    0

    Hi,  

    you can do something like this as well.

    var next = new DynamicNode(m).GetFollowingSiblingNodes();
    var previous = new DynamicNode(m).GetPrecedingSiblingNodes();

    if(next.Any()) {
    @next.Url }

    if(previous.Any()) {
    @previous.Url 
  • Jebastin 42 posts 91 karma points
    Sep 24, 2013 @ 13:37
    Jebastin
    0

    How to get the previous & next node after sorting the list by date?

  • Jebastin 42 posts 91 karma points
    Sep 24, 2013 @ 16:01
    Jebastin
    0

    I also found this solution myself. It may help someone else.

                int NewNodeId = 0, i = 0;
    int[] NodeIdList = new int[mcList.Count];

    foreach (var mc in mcList.OrderByDescending(x => x.GetProperty("publishDate").Value))
    {
    NodeIdList[i] = mc.Id;
    i++;
    }
    i = 0;
    foreach (var mc in mcList.OrderByDescending(x => x.GetProperty("publishDate").Value))
    {
    if (mc.Id == NodeId)
    {
    if (PrevOrNext == "Previous")
    {
    try { NewNodeId = NodeIdList[i - 1]; }
    catch { NewNodeId = 0; }
    }
    else if (PrevOrNext == "Next")
    {
    try { NewNodeId = NodeIdList[i + 1]; }
    catch { NewNodeId = 0; }
    }
    }
    i++;
    }
  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Sep 24, 2013 @ 17:27
    Ismail Mayat
    0

    Jebastin,

    I had to do something similar although not with razor but similar principle. I had list of objects and for a given object in the list i need items next and previous to it. I found this on stackoverflow

            /// <summary>
        /// http://stackoverflow.com/questions/8759849/get-previous-and-next-item-in-a-ienumerable-using-linq
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="matchFilling"></param>
        /// <returns></returns>
        public static IEnumerable<T> FindSandwichedItem<T>(this IEnumerable<T> items, Predicate<T> matchFilling)
        {
            if (items == null)
                throw new ArgumentNullException("items");
            if (matchFilling == null)
                throw new ArgumentNullException("matchFilling");
    
            return FindSandwichedItemImpl(items, matchFilling);
        }
    
        private static IEnumerable<T> FindSandwichedItemImpl<T>(IEnumerable<T> items, Predicate<T> matchFilling)
        {
            using (var iter = items.GetEnumerator())
            {
                T previous = default(T);
                while (iter.MoveNext())
                {
                    if (matchFilling(iter.Current))
                    {
                        yield return previous;
                        yield return iter.Current;
                        if (iter.MoveNext())
                            yield return iter.Current;
                        else
                            yield return default(T);
                        yield break;
                    }
                    previous = iter.Current;
                }
            }
            // If we get here nothing has been found so return three default values
            yield return default(T); // Previous
            yield return default(T); // Current
            yield return default(T); // Next
        }
    
            protected static List<Question> GetSandwichedQuestions(Question currentQuestion)
        {
            var allQuestions = TTContext.Current.CurrentSurvey.Questions;
    
            var sandwichedQuestions = allQuestions.FindSandwichedItem(q => q.QuestionId == currentQuestion.QuestionId).ToList();
    
            return sandwichedQuestions;
        }
    

    This returns list of 3 questions the first one is previous midlde one is current and last one is next. You need to null test on prev and next just in case you are at end of begining of list

    Regards

    Ismail

  • jtemperv 6 posts 26 karma points
    Apr 24, 2020 @ 13:42
    jtemperv
    0

    Sorry for necroing this post, but any good solutions anno 2020? I can't seem to find the Next and Previous members...

  • Sebastian Dammark 581 posts 1385 karma points
    Apr 27, 2020 @ 18:43
    Sebastian Dammark
    0

    I'm doing like this on a V8 solution, using a strongly typed model:

    var siblings = Model.Siblings<DocumentTypeAlias>();
    var prevArticle = siblings.LastOrDefault(x => x.SortOrder < Model.SortOrder);
    var nextArticle = siblings.FirstOrDefault(x => x.SortOrder > Model.SortOrder);
    

    Remember to replace DocumentTypeAlias with your document type alias ;)

Please Sign in or register to post replies

Write your reply to:

Draft