This is finally my first outing with umbraco 7 and Razor having moved from Umbraco 4 & 6 and to say i'm confused is an understatement!
So ok, I'm starting out with what I thought was a very simple problem on an existing site but I've fallen at the first hurdle!!
I have a Partial View Macro File and I want to get all the pages based on a query and populate a list.
This is working fine..
var startNode = Umbraco.Content(startNodeId);
var selection = startNode.Descendants().Where("Visible && DocumentTypeAlias != @0", "Group");
<ul>
@foreach (var item in selection.OrderBy("Name"))
{
<option data-id="@item.Id" value="@item.Name">@item.Name</option>
}
</ul>
But.. I need to filter this list.
There are some nodes that have the same name, and I only need to show one of them so I am trying to add all the nodes to a new list but only if a node with the same name doesn't already exist.
So I have added this and then am trying to add these to the list.
var DistinctSelection = Array[]
@foreach (var item in selection)
{
@if(!DistinctSelection.Any(n=>n.Name==item.Name)){
@DistinctSelection.Add(item); }
}
I am really not sure if my syntax is correct but when I run it nothing happens. No error, nothing! I just get an empty list.
Filtering Nodes and Adding to New List Razor
Hi
This is finally my first outing with umbraco 7 and Razor having moved from Umbraco 4 & 6 and to say i'm confused is an understatement!
So ok, I'm starting out with what I thought was a very simple problem on an existing site but I've fallen at the first hurdle!!
I have a Partial View Macro File and I want to get all the pages based on a query and populate a list. This is working fine..
But.. I need to filter this list. There are some nodes that have the same name, and I only need to show one of them so I am trying to add all the nodes to a new list but only if a node with the same name doesn't already exist.
So I have added this and then am trying to add these to the list.
I am really not sure if my syntax is correct but when I run it nothing happens. No error, nothing! I just get an empty list.
My questions are:
Any help or guidance would be much appreciated!
Thanks
Bex
I have found a way of doing it!!
Courtesy of this post:
https://our.umbraco.org/forum/developers/razor/19020-Is-there-something-like-%27GroupBy%27
It doesn't involve adding them to a list or array so I'd still actually like to know if thats possible.. but here's how I've done it:
Hi Bex,
Have you tried with DistinctBy. Should be cleaner and faster.
Cheers,
/Lucio
Hi Lucio
I just tried this but this just gives the empty dropdown again. Is there anyway of getting feedback on errors or when something doesnt work?
Is it working if replace this:
For this:
You are not having any compilation error?
Ups, I have just see that you are working with a dynamic object (Umbraco.Content instead of Umbraco.TypedContent) so you need to use the next:
Sorry about that xD
Still give blank dropdown with no error :( Least i got it working. Really not liking these no errors. Am I doing this wrong?
Hi Bex and Lucio,
Problem that you are using dynamic types, if you will rewrite it to strongly typed - you will see errors and it will be easier to work with it.
Thanks,
Alex
is working on a reply...