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;
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
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.
I found the solution myself. I've given the solution here; it may help to someone.
Hello,
Did you try something like this?
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
Hi,
you can do something like this as well.
How to get the previous & next node after sorting the list by date?
I also found this solution myself. It may help someone else.
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
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
Sorry for necroing this post, but any good solutions anno 2020? I can't seem to find the Next and Previous members...
I'm doing like this on a V8 solution, using a strongly typed model:
Remember to replace
DocumentTypeAlias
with your document type alias ;)is working on a reply...