Copied to clipboard

Flag this post as spam?

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


  • Mathias Valentin 60 posts 208 karma points
    Jul 13, 2017 @ 10:37
    Mathias Valentin
    0

    Nested content as a macro parameter

    Hi!

    Is it possible to use a nested content property as a macro parameter? Like so:

    var items = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>("items");
    @Umbraco.RenderMacro("SomeMacro", new {
       items = items
    });
    

    My question is how do I parse the <IEnumerable<IPublishedContent>> to the macro as a parameter?

    This is what I have so far in the macro view (not working):

    var items = Model.MacroParameters["items"] as IEnumerable<IPublishedContent>;
    foreach(var item in items) {
     ...
    }
    

    Thanks!!!

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jul 13, 2017 @ 16:18
    Alex Skrypnyk
    0

    Hi Mathias

    You have to cast macro param like that:

    var items = Model.MacroParameters["items"] as IEnumerable<IPublishedContent>;
    

    Thanks,

    Alex

  • Mathias Valentin 60 posts 208 karma points
    Jul 14, 2017 @ 08:51
    Mathias Valentin
    0

    Hi Alex! Im getting System.NullReferenceException: Object reference not set to an instance of an object. on the foreach line. Does it matter which parameter type items is set to?

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jul 14, 2017 @ 08:54
    Alex Skrypnyk
    1

    Hi Mathias

    I have an idea, why did you pass CurrentPage field to macro?

    You can use in the macro:

    var items = CurrentPage.GetPropertyValue<IEnumerable<IPublishedContent>>("items");
    

    Thanks,

    Alex

  • Mathias Valentin 60 posts 208 karma points
    Jul 14, 2017 @ 09:11
    Mathias Valentin
    0

    Yeah that would work, but I didn't mention that the nested content is a property on another nested content, so CurrentPage is not an option :(

    var items = OtherNestedContentItem.GetPropertyValue<IEnumerable<IPublishedContent>>("items");
    @Umbraco.RenderMacro("SomeMacro", new { items = items});
    

    My question is how do I parse the <IEnumerable<IPublishedContent>> to the macro as a parameter or if you have another solution to this problem?

Please Sign in or register to post replies

Write your reply to:

Draft