Copied to clipboard

Flag this post as spam?

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


  • edwich44 15 posts 71 karma points
    Jan 13, 2015 @ 13:29
    edwich44
    0

    Can you pass IPublishedContent type into a Macro?

    Hi guys,

    I have been trying to pass in a IEnumerable

    @Umbraco.RenderMacro("Macro", new { content= @Model.Content.Children  }
    

    @Model.Content.Children returns IEnumerable

    On my macro I try to do:-

    IEnumerable<IPublishedContent> content = (IEnumerable<IPublishedContent>)@Model.MacroParameters["content"]
    

    But I get this error:-

    Unable to cast object of type 'System.String' to type System.Linq.OrderedEnumerable`2[Umbraco.Core.Models.IPublishedContent,System.Int32]

    So does that mean that you can only pass strings and ints into a macro or am I doing something wrong?

    Cheers

  • Valerie 67 posts 163 karma points
    Jan 13, 2015 @ 13:39
    Valerie
    0

    Does this have to be a macro rather than a Html.Partial? What is it doing?

  • edwich44 15 posts 71 karma points
    Jan 13, 2015 @ 13:48
    edwich44
    0

    @Valerie

    The only reason why its a macro is because we had a rule that if we are going to pass values into a partial view then we will create macros instead, however, if its only possible to pass class types into a Partial views then I guess we will have to use them instead.

    The macro was created to display content node properties so - IPusblishedContent.GetPropertyValue(""), so nothing that cant go into a partial view I guess.

  • Valerie 67 posts 163 karma points
    Jan 13, 2015 @ 13:51
    Valerie
    0

    This is very strange, it's passing the type of the object as a string and I can't seem to be able to get to the actual object...

  • edwich44 15 posts 71 karma points
    Jan 13, 2015 @ 13:54
    edwich44
    0

    @Valerie - exactly. Thats the issue im having. I guess, we could just pass in the id of the content node and in the macro do:-

    IPublishedContent content = Umbraco.TypedContent(@Model.MacroParameters["content"].toString())
    

    I was just wondering if it is possible to pass class types into the macro as it seems you can only pass ints and strings but I might be wrong.

  • Valerie 67 posts 163 karma points
    Jan 13, 2015 @ 13:56
    Valerie
    0

    I tried actually running the macro from the Rich Text Editor and selecting multiple nodes that way just to see if that worked but it just does the same thing.. since it's there as a type of macro parameter there surely must be a way to do it...

    I'm struggling to find docs about how to deserialise the data from the Multi Content Picker parameter type.

  • Valerie 67 posts 163 karma points
    Jan 13, 2015 @ 13:59
    Valerie
    0

    Oh, turns out I just totally failed to read. It's multiple content types not content... but actually it has the same problem.

  • Valerie 67 posts 163 karma points
    Jan 13, 2015 @ 14:00
    Valerie
    0

    Actually, what "type" is your "Content" parameter in your macro? I can't see a Multiple IPublishedContent Picker.

  • edwich44 15 posts 71 karma points
    Jan 13, 2015 @ 14:13
    edwich44
    0

    @Valarie, thats a good point. I just put it as ContentPicker but I tried other types and they all seem to be passed in as a string.

    I tried the multiple content type to see what it rendered in the umrbaco tools, and it does this - ["1117","1120"]- Which you probably already know.

    Its then having to convert that into a collection of ints perhaps. So maybe we will have to write some logic to loop through each char of that string and pull out the numbers.

  • Valerie 67 posts 163 karma points
    Jan 13, 2015 @ 14:15
    Valerie
    0

    Yeah sounds like a plan :)

  • edwich44 15 posts 71 karma points
    Jan 13, 2015 @ 14:19
    edwich44
    0

    @Valerie - or like you mentioned before, you could use Partial views instead as you can pass in class types etc into them. But there could be a way via macros, I just dont know.

  • Valerie 67 posts 163 karma points
    Jan 13, 2015 @ 14:31
    Valerie
    0

    I'd just use a partial :) I normally only use macros these days if I am interacting with the Rich Text Editor but I'm not a huge macro user.

    Is there any benefit to using a Macro instead when you have parameters?

  • edwich44 15 posts 71 karma points
    Jan 13, 2015 @ 14:35
    edwich44
    0

    @Valerie - We use them so that our front end developers can easily add a macro and pass values into them via the admin tools

  • Valerie 67 posts 163 karma points
    Jan 13, 2015 @ 14:41
    Valerie
    0

    Ah right! Colour me learned :)

Please Sign in or register to post replies

Write your reply to:

Draft