Copied to clipboard

Flag this post as spam?

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


  • Ivan 165 posts 543 karma points
    May 27, 2013 @ 06:22
    Ivan
    0

    Get page id in razor macro

    Hi guys.

    Razor macro is called from XSLT tempalte like this:

    <xsl:value-of select="umbraco.library:RenderMacroContent($razorMacro, $nodeId)"  />

    How do I then retrieve $nodeId parameter in Razor?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 29, 2013 @ 18:27
    Jeavon Leopold
    0

    Hi Ivan,

    As this parameter sets the page context, you could get its value from @Model.Id in your Razor script (assuming you are using a Razor Macro rather than Mvc)

    Hope that helps you?

    Thanks,

    Jeavon

  • Ivan 165 posts 543 karma points
    May 29, 2013 @ 19:16
    Ivan
    0

    Hi Jeavon.

    Nope, it does't work for me. Model.Id always returns Id of current page. Perhaps I'm using Razor Mvc (or whatever it is). Any other thoughs?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 29, 2013 @ 20:02
    Jeavon Leopold
    100

    Yup, you are right it is odd, DynamicNode used for @Model is bypassing this context parameter, the RenderMacroContent method was written long before Razor of course.

    So I just did a quick POC and the workaround is to create a macro parameter and to retrieve that value in your Razor instead:

    In my XSLT:

    <xsl:variable name="nodeId">1053</xsl:variable>
    <xsl:value-of select="umbraco.library:RenderMacroContent(concat('&lt;?UMBRACO_MACRO macroAlias=&quot;testRazor&quot; nodeId=&quot;',$nodeId,'&quot;&gt;&lt;/?UMBRACO_MACRO&gt;'),$nodeId)" disable-output-escaping="yes"/>

    In my Razor:

    <p>Parameter Id: @Parameter.nodeId</p>

    You must add the nodeId parameter to your Macro definition also.

    Hopefully that will do the trick?

  • Ivan 165 posts 543 karma points
    May 29, 2013 @ 20:09
    Ivan
    1

    Alright. Thanks, Jeavon. I'll give it a try.

Please Sign in or register to post replies

Write your reply to:

Draft