Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
How do i get the position of a node in a node-set?
Equal to position in xslt?
Hi Claus
What are you trying to do and do you have some code you could share?
/Jan
Hi Claus,
To get the position of a node in a node-set, in Razor you can use the .Index() method. The index is zero based.
<a href="@item.Url" class="[email protected]()">@item.Name</a>
So with is you will get items, in e.g a foreach, where each item gets the class name item-0, item-1 and so on, like you know it in XSLT.
In razor you have some simple IsHelpers to get e.g the first item and the last item, and isEven isOdd an so on.
Here is a list of the IsHelpers:
.IsFirst(valueIfTrue[,valueIfFalse]).IsNotFirst(valueIfTrue[,valueIfFalse]).IsLast(valueIfTrue[,valueIfFalse]).IsNotLast(valueIfTrue[,valueIfFalse]).IsPostion(int,valueIfTrue[,valueIfFalse]).IsNotPosition(int,valueIfTrue[,valueIfFalse]).IsModZero(int,valueIfTrue[,valueIfFalse]).IsNotModZero(int,valueIfTrue[,valueIfFalse]).IsEven(valueIfTrue[,valueIfFalse]).IsOdd(valueIfTrue[,valueIfFalse]).IsEqual(DynamicPublishedContent,valueIfTrue[,valueIfFalse]).IsNotEqual(DynamicPublishedContent,valueIfTrue[,valueIfFalse]).IsDescendant(DynamicPublishedContent,valueIfTrue[,valueIfFalse]).IsDescendantOrSelf(DynamicPublishedContent,valueIfTrue[,valueIfFalse]).IsAncestor(DynamicPublishedContent,valueIfTrue[,valueIfFalse]).IsAncestorOrSelf(DynamicPublishedContent,valueIfTrue[,valueIfFalse])
Hope this helps,
/Dennis
Ok, so here's some code
@{ var navigate = Model.Content.GetPropertyValue<ArchetypeModel>("slider"); foreach (var fieldset in navigate) { if (fieldset.Equals(navigate.First())) { <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> } else { <li data-target="#carousel-example-generic" data-slide-to="@fieldset.Index()"></li> } } }
So i want the "data-slide-to" to use the "Index()". But the above code, using Dennis' example doesnt work...
What if you use simple .Position() method does this do any difference.
@{ var navigate = Model.Content.GetPropertyValue<ArchetypeModel>("slider"); foreach(var fieldset in navigate) { if(fieldset.Equals(navigate.First())) { <li data-target="#carousel-example-generic" data-slide-to="0"class="active"></li> } else { <li data-target="#carousel-example-generic" data-slide-to="@fieldset.Position()"></li> } }}
Hope so.
Nope, returns error
I was thinking of could you do it this way?
@{ var navigate = Model.Content.GetPropertyValue<ArchetypeModel>("slider"); int i = 1; foreach(var fieldset in navigate){ if(fieldset.Equals(navigate.First())) { <li data-target="#carousel-example-generic" data-slide-to="0"class="active"></li> } else { <li data-target="#carousel-example-generic" data-slide-to="@i"></li> } i++; }}
If not then I am almost empty for ideas on how you try to get it work.
Succes! Thanks!
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Get position of element in a "node-set"
How do i get the position of a node in a node-set?
Equal to position in xslt?
Hi Claus
What are you trying to do and do you have some code you could share?
/Jan
Hi Claus,
To get the position of a node in a node-set, in Razor you can use the .Index() method. The index is zero based.
So with is you will get items, in e.g a foreach, where each item gets the class name item-0, item-1 and so on, like you know it in XSLT.
In razor you have some simple IsHelpers to get e.g the first item and the last item, and isEven isOdd an so on.
Here is a list of the IsHelpers:
Hope this helps,
/Dennis
Ok, so here's some code
So i want the "data-slide-to" to use the "Index()". But the above code, using Dennis' example doesnt work...
Hi Claus,
What if you use simple .Position() method does this do any difference.
Hope so.
/Dennis
Nope, returns error
Hi Claus,
I was thinking of could you do it this way?
If not then I am almost empty for ideas on how you try to get it work.
/Dennis
Succes! Thanks!
is working on a reply...