I am trying to learn razor, so am trying to convert a simple xslt macro to a razor partial, but not having much luck. I've looked at 'Umbraco 4.7 Razor Feature Walkthrough' and checked the forums, google, etc, and not been able to come up with a working solution. (I am fairly new to razor and paritals and pretty much all things MVC, understand the concepts, just not much working experience)
My xslt looks like this (basically selects all items of doc type News in the content tree)
Ok, so that is happening because it cannot locate your NewsFolder, can I check that your tree structure is as follows (with each item being a parent node of the next)
But is there a way to make it better match the xslt? That is, get the actual root node of the content tree (vs the 1st level), then get something under it.
No I do not believe that there is a way to specifically select the root node because it is not a real node (it has no properties). You can select all the siblings of your current root node (all the children under the root node). e.g.
var allRootNodeChildren = Model.Content.AncestorOrSelf(1).Siblings();
And from there you have access to every node within the Umbraco install.
xslt to razor help
I am trying to learn razor, so am trying to convert a simple xslt macro to a razor partial, but not having much luck. I've looked at 'Umbraco 4.7 Razor Feature Walkthrough' and checked the forums, google, etc, and not been able to come up with a working solution. (I am fairly new to razor and paritals and pretty much all things MVC, understand the concepts, just not much working experience)
My xslt looks like this (basically selects all items of doc type News in the content tree)
How do I create partial to do the same thing?
Hi Karen,
In a strongly typed partial it would be like this:
Then to render in your View (assuming your partial is callewd News)
Hope that helps get your started.
Thanks,
Jeavon
Hi Jeavon,
Thanks!
I tried this, but am getting an error on the following line:
Error:
Exception Details: System.InvalidOperationException: Sequence contains no elements
So an additional queston, is how to avoid the page throwing such an error message?
Hi Karen,
Ok, so that is happening because it cannot locate your NewsFolder, can I check that your tree structure is as follows (with each item being a parent node of the next)
Content (root)
Homepage
NewsFolder
News
News
OtherPage
Or is NewsFolder a sibling of Homepage?
Thanks,
Jeavon
Hi,
It is:
Content (root)
- Homepage
-- Some Pages
- NewsFolder
-- News
The xslt is working, so trying to base it off that
Thanks
Ah cool, ok so try:
OK - that works.
But is there a way to make it better match the xslt? That is, get the actual root node of the content tree (vs the 1st level), then get something under it.
In xslt:
vs this seems to get 1 level under
So I want something like this (but this doesn't work)
Hi Karen,
No I do not believe that there is a way to specifically select the root node because it is not a real node (it has no properties). You can select all the siblings of your current root node (all the children under the root node). e.g.
And from there you have access to every node within the Umbraco install.
Hope that's helpful?
Thanks,
Jeavon
OK thank you very much for your help!
You're very welcome! Thanks for your "High five!" above, that was my 1000th karma point :-)
Hi Karen,
I just discovered that in Umbraco v6.1+ there is a new helper method called TypedContentAtXPath that can do exactly what you need:
The method lets you execute a XPath expression against your entire tree.
Hope that's helpful!
Jeavon
Awesome! Thanks!
is working on a reply...