You can use "OrderBy" to sort alphabetically in Razor. To create sub-groups you can use nested "foreach" loops, the first one to grab the main group and the inner loop to get the children.
thank you very much for the code you provided. I tried to run it, i am receiving this error message: error CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
I don't know if I am doing something wrong or not. I will try to investigate it.
That example is using the strongly typed IPublishedContent (@Model.Content) in an MVC View. Are you using the DynamicPublishedContent? or razor in a MacroScript?
if i recall correctly the DynamicNodeList has and property called "InGroupsOf" that might take and lamda espression as a string! (yes as a string) u can mostlily do it with "regular" lamda but in razormacros it's syntax is ..... u have to passe the expression as a string to the method.
something like myList.Where("NodeTypeAlias == \"something\" and Field == \"Myfield \""); ugly yes :)
I will try to dig up some examples from old projects.
to get this to work you could order the nodes ( with OrderBy ) and then use a second loop to format the grouping.
//current letter value to compare
string letter=String.Empty;
var nodes =Model.Descendants().Where("Visible").OrderBy("lastName");
@foreach (var item in nodes)
{
//if the letter has changed display header
if(!letter.Equals(item.Name.Substring(0,1))
{
letter =item.Name.Substring(0,1);
<div class="heading">@letter</div>
}
<div>@item.Name</div>
}
Group Sorting Alphabetically - Razor
Is it possible to list subnodes in alphabetical groups in razor? e.g.
Adidas, Armani
Bvlgari, Burton
etc.. I have found this topic http://our.umbraco.org/forum/developers/xslt/17314-How-to-Group-Sort-nodes-alphabetically where it was possible to do in XSLT.
I would really appreciate any guidance for Razor example. I have both umbraco version 4 and 6 projects running.
You can use "OrderBy" to sort alphabetically in Razor. To create sub-groups you can use nested "foreach" loops, the first one to grab the main group and the inner loop to get the children.
Here is an example that we are using in mvc razor. It is using OrderBy and GroupBy to group by the first letter of each node.
and then loop over the groups
Dallas
Hey Dallas,
thank you very much for the code you provided. I tried to run it, i am receiving this error message:
error CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
I don't know if I am doing something wrong or not. I will try to investigate it.
Best,
That example is using the strongly typed IPublishedContent (@Model.Content) in an MVC View. Are you using the DynamicPublishedContent? or razor in a MacroScript?
Dallas
Yes, I am trying to run razor in a Macroscript.
if i recall correctly the DynamicNodeList has and property called "InGroupsOf" that might take and lamda espression as a string! (yes as a string) u can mostlily do it with "regular" lamda but in razormacros it's syntax is ..... u have to passe the expression as a string to the method.
something like myList.Where("NodeTypeAlias == \"something\" and Field == \"Myfield \""); ugly yes :)
I will try to dig up some examples from old projects.
This forum post discusses the razor GroupBy and InGroupsOf functions
to get this to work you could order the nodes ( with OrderBy ) and then use a second loop to format the grouping.
Dallas
thanks Dallas,
I will check it out.
is working on a reply...