I recon it's because one way you are only get the data which has been published and cached (and stored inside the umbraco.config file) (first example). In the second example you are using a Document you get the data which is stored inside the database as your using complex querying.
It is something related to CurrentPage extensions methods because locally they are not being recognised. Because errors are occuring alos on breadrcumbs for example, etc..
When you are using @CurrentPage, you are using dynamic razor. WIth dynamic you get conciseness and with strongly typed version you get intellisense. To get current page in Razor using the dynamic implementation you will have to use @CurrentPage. To get current page in strongly typed Razor you need Model.Content.
The problem is not actually in that line of code, altough you your suggestion has not worked tough. Locally and on another server, it is working fine but on a particular server it is not working, showing the below error.
Also, there are some related errors, for example the breadcrumbs code snippet that umbraco generates automatically. Locally and on another server, no errors occur but on another server, the breadcrumbs are not being rendered. Instead the following error is occuring.
Alos, there are errors when trying to render the navigation menu, for example.
These such errors are related because related to CurrentPage extensions methods. As I said, only when publihsing to a particular server, because locally and on smarterasp.net, the site is working fine.
Or, if you're using Umbraco 7.2.2 (with the new QueryBuilder tool) you can simplify even more with:
foreach (var item in CurrentPage.FirstChild("HomePageBanner").Children)
In fact, both .Children() with a document type alias as a parameter and .FirstChild() with a doctype alias as a parameter are new and super helpful. Prior to that (and as you'll see in other, older forum posts) you'd have had to use a pluralized form of the document type alias to get children of that type, which wasn't at all an obvious meaning, or you'd have to use a .Where claus:
foreach (var item in CurrentPage.HomePageBanners.First().Children
or foreach (var item in CurrentPage.Children.Where("NodeTypeAlias == \"HomePageBanner\"").First().Children
The new options are much clearer to my thinking. And, with the QueryBuilder button on the toolbar of the Templates and Partial Views sections you can always click your way through to discover the best selection. Great for learning. Great for saving typing. Great for avoiding typos. :)
@Simon - What version of Umbraco are you running? Was it an upgrade from v6? Are you in either the 'Templates' or 'Partial Views' or 'Partial View Macro Files' sections of Umbraco, or in another section where you're using this Razor code?
CurrentPage and Model
Hi all,
I am a bit confusing about such issue that have encountered.
I have published a site online.
Having this the below line of code:
was throwing an error that "Cannot invoke a non delegate type"
Then I changed that the above code to the below line of code:
and it works.
This happened only online, because locally, both of them worked.And also quire sure that all files were uploaded on server.
any ideas please?
Thank you in advance.
Kind Regards.
Hi Simon,
Have you tried refreshing the node XML might be a caching.
http;//YOURDOMAIN/Umbraco/dialogs/republish.aspx?xml=true
I recon it's because one way you are only get the data which has been published and cached (and stored inside the umbraco.config file) (first example). In the second example you are using a Document you get the data which is stored inside the database as your using complex querying.
Hi John.
No Success :/
It is something related to CurrentPage extensions methods because locally they are not being recognised. Because errors are occuring alos on breadrcumbs for example, etc..
Any idea pls?
thank you.
Kind Regards
Hi John,
Sry, there is a mistake in the first comment.
Error is being thrown when having
foreach(var item inCurrentPage.Content.Children("HomePageBanner").First().Children)
Sry for the mistake.
any ideas pls?
thank you.
Hi Simon,
When you are using @CurrentPage, you are using dynamic razor. WIth dynamic you get conciseness and with strongly typed version you get intellisense. To get current page in Razor using the dynamic implementation you will have to use @CurrentPage. To get current page in strongly typed Razor you need Model.Content.
Try to see this documentation https://our.umbraco.org/documentation/reference/templating/macros/Partial-View-Macros/#PartialViewMacros and https://our.umbraco.org/documentation/Reference/Templating/Mvc/views
And this should work.
Hope this helps, if you have further questions, keep ask and I will try to help you.
/Dennis
CurrenPage is already your dynamic object. So you don't need to do CurrentPage.Content.
Try this:
Jeroen
Hi Dennis and Jeroen,
thank you for your reply.
The problem is not actually in that line of code, altough you your suggestion has not worked tough. Locally and on another server, it is working fine but on a particular server it is not working, showing the below error.
Also, there are some related errors, for example the breadcrumbs code snippet that umbraco generates automatically. Locally and on another server, no errors occur but on another server, the breadcrumbs are not being rendered. Instead the following error is occuring.
Alos, there are errors when trying to render the navigation menu, for example.
These such errors are related because related to CurrentPage extensions methods. As I said, only when publihsing to a particular server, because locally and on smarterasp.net, the site is working fine.
Any Ideas pls?
thank you in advance.
Kind regards.
Or, if you're using Umbraco 7.2.2 (with the new QueryBuilder tool) you can simplify even more with:
foreach (var item in CurrentPage.FirstChild("HomePageBanner").Children)
In fact, both .Children() with a document type alias as a parameter and .FirstChild() with a doctype alias as a parameter are new and super helpful. Prior to that (and as you'll see in other, older forum posts) you'd have had to use a pluralized form of the document type alias to get children of that type, which wasn't at all an obvious meaning, or you'd have to use a .Where claus:
foreach (var item in CurrentPage.HomePageBanners.First().Children
or
foreach (var item in CurrentPage.Children.Where("NodeTypeAlias == \"HomePageBanner\"").First().Children
The new options are much clearer to my thinking. And, with the QueryBuilder button on the toolbar of the Templates and Partial Views sections you can always click your way through to discover the best selection. Great for learning. Great for saving typing. Great for avoiding typos. :)
cheers,
doug.
@Simon - What version of Umbraco are you running? Was it an upgrade from v6? Are you in either the 'Templates' or 'Partial Views' or 'Partial View Macro Files' sections of Umbraco, or in another section where you're using this Razor code?
Thanks, that info will help a lot.
cheers,
doug.
Hi Douglas,
I am using Umbraco 7.2.1 and I didn't make any upgrade.
The code is in the Template views.
thank you for trying to help me.
Kind Regards.
Issue http://issues.umbraco.org/issue/U4-6438 - discuss there!
is working on a reply...