I would like to return children from a level 2 node, the following code returns only the level 2 child. The code is a partial view,any assistance would be great ?
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
var root = Model.Content.AncestorOrSelf(1);
int docId = Model.Content.Id;
var nodes = root.Children.Where(x => x.Level == 2 && x.Name == "Departments");
}
@{
foreach(var node in nodes){
<h3><a href="@node.Url">@node.Name</a></h3>
<p>@(Umbraco.Truncate(node.GetPropertyValue("mainContent").ToString(), 200, true))</p>
}
Taking your code from above, I'd added some comments about what the x.Level code segment is doing. I've also removed the x.Name == "Departments" as you are effectively filtering for a single node by using the "Name" to filter against in that manner.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
var root = Model.Content.AncestorOrSelf(1);
int docId = Model.Content.Id;
//The x.Level == 2 means only get items where their level is equal to 2
//If you put x.Level <= 4 it would get everything upto level 4 for example
var nodes = root.Children.Where(x => x.Level == 2);
}
@{
foreach(var node in nodes){
<h3><a href="@node.Url">@node.Name</a></h3>
<p>@(Umbraco.Truncate(node.GetPropertyValue("mainContent").ToString(), 200, true))</p>
}
}
Saying that, however, if you are trying to list all of children of the "Departments" node I would do the following, working on the assumption that the current page is NOT the departments page.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
var root = Model.Content.Site();
///Personally I would rather be using a document type alias filter here as this has limited reusability, using the First Linq expression to get the first occurrence of the Departments node as I don't believe it should be possible to have multiple.
var departmentNode = root.Children.First(x => x.Name == "Departments");
}
@{
foreach(var node in departmentNode.Children){
<h3><a href="@node.Url">@node.Name</a></h3>
<p>@(Umbraco.Truncate(node.GetPropertyValue("mainContent").ToString(), 200, true))</p>
}
}
No problem, to be fair it actually depends on exactly what you are trying to achieve.
Let's say for example you have a the following document types:
Departments Repository (alias of departmentRepository)
Department (alias of department)
Where Department(s) can only be created under a DepartmentsRepository node.
Then to make the partial view re-usable I would structure it like this:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
var repositoryNode = Model.Content.AncestorOrSelf("departmentRepository");
//Make sure we have a repository to work with
if(repositoryNode != null){
foreach(var department in repositoryNode.Children.Where(x=>x.DocumentTypeAlias.Equals("department")))
{
<h3><a href="@childnode.Url">@childnode.Name</a></h3>
<p>@(Umbraco.Truncate(childnode.GetPropertyValue("mainContent").ToString(), 200, true))</p>
}
}
}
This was you can re-use your partial view on any page that is underneath a departmentRepository and it will render out the entire list.
As I mentioned at the start, it really depends on use-case though, the more you can make things flexible and reusable, often the easier it can be :-)
partial view return children
Dear Umbraco Team
I would like to return children from a level 2 node, the following code returns only the level 2 child. The code is a partial view,any assistance would be great ?
}
Hi Dibs,
Taking your code from above, I'd added some comments about what the x.Level code segment is doing. I've also removed the x.Name == "Departments" as you are effectively filtering for a single node by using the "Name" to filter against in that manner.
Saying that, however, if you are trying to list all of children of the "Departments" node I would do the following, working on the assumption that the current page is NOT the departments page.
Cheers Nik
i did get what i wanted by adding a nested foreach loop like so:
But taking on your point how i have coded it, would your way still be better ?
Thanks Dibs
Hi Dibs,
No problem, to be fair it actually depends on exactly what you are trying to achieve.
Let's say for example you have a the following document types:
Departments Repository (alias of departmentRepository)
Department (alias of department)
Where Department(s) can only be created under a DepartmentsRepository node.
Then to make the partial view re-usable I would structure it like this:
This was you can re-use your partial view on any page that is underneath a departmentRepository and it will render out the entire list.
As I mentioned at the start, it really depends on use-case though, the more you can make things flexible and reusable, often the easier it can be :-)
Nik
Cheers Nik : )
is working on a reply...