Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon 692 posts 1068 karma points
    Mar 24, 2015 @ 12:35
    Simon
    1

    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: 

    foreach (var item in Model.Content.Children("HomePageBanner").First().Children)

    was throwing an error that "Cannot invoke a non delegate type"

    Then I changed that the above code to the below line of code:

     foreach (var item in Model.Content.Children.Where(x => x.DocumentTypeAlias == "HomePageBanner").First().Children)

    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.

  • John Churchley 272 posts 1258 karma points c-trib
    Mar 24, 2015 @ 14:15
    John Churchley
    0

    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.

     

  • Simon 692 posts 1068 karma points
    Mar 24, 2015 @ 16:03
    Simon
    0

    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

  • Simon 692 posts 1068 karma points
    Mar 24, 2015 @ 16:07
    Simon
    0

    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.


  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 24, 2015 @ 16:19
    Dennis Aaen
    2

    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.

    foreach(var item in CurrentPage.Children("HomePageBanner").First().Children)

    Hope this helps, if you have further questions, keep ask and I will try to help you.

    /Dennis

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 24, 2015 @ 16:20
    Jeroen Breuer
    2

    CurrenPage is already your dynamic object. So you don't need to do CurrentPage.Content.

    Try this:

    foreach(var item in CurrentPage.Children("HomePageBanner").First().Children)

    Jeroen

  • Simon 692 posts 1068 karma points
    Mar 24, 2015 @ 16:35
    Simon
    0

    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.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Mar 24, 2015 @ 16:38
    Douglas Robar
    3

    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.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Mar 24, 2015 @ 16:41
    Douglas Robar
    0

    @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.

  • Simon 692 posts 1068 karma points
    Mar 24, 2015 @ 16:43
    Simon
    0

    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.

  • Stephen 767 posts 2273 karma points c-trib
    Mar 09, 2016 @ 10:23
    Stephen
    0
Please Sign in or register to post replies

Write your reply to:

Draft