Copied to clipboard

Flag this post as spam?

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


  • suzyb 474 posts 932 karma points
    Sep 10, 2013 @ 15:49
    suzyb
    0

    Confused over when DynamicNodeList is returned

    I'm trying to get a list of news articles so I can loop them.  But I can't seem to get my code to work correctly.  This is my code.

    DynamicNode newsHome = Model.AncestorOrSelf("NewsListingPage");
    DynamicNodeList articles = newsHome.Descendants("NewsArticle").Where("Visible").OrderBy("articleDate desc");

    That however gives me this error 'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Enumerable.Where<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,bool>)' has some invalid arguments

    But when I change the second line to this it seems to work 

    DynamicNodeList articles = Model.Descendants("NewsArticle").Where("Visible").OrderBy("articleDate desc");

    As far as I understand Model is of type DynamicNode the same as the newsHome variable I create.  So why does Model work and newsHome not work.

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 10, 2013 @ 18:20
    Jeroen Breuer
    0

    You could try to make newsHome dynamic. So try this:

    dynamic newsHome = Model.AncestorOrSelf("NewsListingPage");
    dynamic articles = newsHome.Descendants("NewsArticle").Where("Visible").OrderBy("articleDate desc");

    Jeroen

  • Moran 285 posts 934 karma points
    Sep 10, 2013 @ 18:50
    Moran
    0

    Great question I ran into this myself and used the same work around as you did or used this:

    DynamicNode newsHome = Model.AncestorOrSelf("NewsListingPage");
    IEnumerable<DynamicNode> articles = newsHome.Descendants(p => p.NodeTypeAlias == "NewsArticle").Where(p => p.Visible);
    
  • suzyb 474 posts 932 karma points
    Sep 10, 2013 @ 22:25
    suzyb
    0

    dynamic works, although I was using DynamicNode to keep things strongly typed.

    Would still like to know what the difference is though.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 11, 2013 @ 09:19
    Jeroen Breuer
    0

    It's not called DynamicNode for a reason ;-). It has some features that only work when you use it dynamic instead of strongly typed.

    I don't know what version you are on, but since 4.10 you can use IPublishedContent which is strongly typed and a lot better than DynamicNode. More info here: http://our.umbraco.org/documentation/Reference/Mvc/querying

    Jeroen

  • suzyb 474 posts 932 karma points
    Sep 11, 2013 @ 10:57
    suzyb
    0

    The code is in a razor macro and afaik IPublishedContent was just available on views.

    All these different types that are more or less the same confuse me ;)

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 11, 2013 @ 11:30
    Douglas Robar
    110

    Yes, it's a bit confusing during the period both WebForms and MVC are available. Here's my 'rule of thumb' that I hope helps:

    Developer section > Scripting Files == DynamicNode razor.
    Developer section > Partial View Macro Files == "Real" Razor.
    Settings section > Template (Webforms mode) == (aka, MasterPages) DynamicNode razor
    Settings section > Template (MVC mode) == (aka, Views) "Real" razor
    Settings section > Partial Views == "Real" razor.

    You can use either Scripting Files or Partial View Macro Files for you Umbraco macros with either templating mode, which makes transitioning much easier.

    See the various razor cheatsheets for the different Razor modes as each is subtly different in syntax in a few places.

    DynamicNode razor: is dynamic (@Model.BodyText) unless you do a lot of work to force it otherwise. Let it be dynamic and use 'real' razor if you need strongly typed is my advice
    http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet

    Real razor: can be either strongly typed (@Model.Content.BodyText) or dynamic (@CurrentPage.BodyText)
    http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

    Hope this helps.

    cheers,
    doug.

  • suzyb 474 posts 932 karma points
    Sep 11, 2013 @ 18:40
    suzyb
    0

    Nice reply Douglas. Thanks.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Sep 11, 2013 @ 21:35
    Dan Diplo
    1

    Doug, is this bit correct?

    Real razor: can be either strongly typed (@Model.Content.BodyText) or dynamic (@CurrentPage.BodyText)

    Should it be, for strongly typed:

    @Model.Content.GetPropertyValue("bodyText")

    or

    var bodyText = Model.Content.GetPropertyValue<HtmlString>("bodyText");

    Or am I missing a way of getting strongly typed custom properties?

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 11, 2013 @ 21:39
    Douglas Robar
    0

    Ooops! You're totally correct, Dan. I'll edit my post to show the strongly-typed example correctly. Thanks for the eyes and correction.

    cheers,
    doug.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 11, 2013 @ 21:42
    Douglas Robar
    0

    Well I would edit my post to correct but that doesn't seem to be working. :(

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Sep 11, 2013 @ 21:44
    Dan Diplo
    0

    Has the edit ever worked properly? ;-)

    I'm just pleased I managed to correct you, Doug - not something that happens often when it comes to Umbraco! Let me have my 5 mins fame :)

Please Sign in or register to post replies

Write your reply to:

Draft