Copied to clipboard

Flag this post as spam?

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


  • Marc Love (uSkinned.net) 447 posts 1789 karma points
    Dec 11, 2012 @ 16:53
    Marc Love (uSkinned.net)
    0

    Insert Macro inside razor script

    I have a macro usercontrol that contains a typical asp.net form. Is it possible to have a razor script that has some logic in it to insert this macro depending on some condition. If so what is the code for adding a macro to a razor script?

    Hopefully this makes sense!!!

    Cheers

  • Marc Love (uSkinned.net) 447 posts 1789 karma points
    Dec 11, 2012 @ 16:59
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Dec 11, 2012 @ 17:52
    Jeroen Breuer
    0

    You can't have a UserControl macro on a Razor Macro (unless it doesn't have any postbacks). Only the other way around works. I had the same problem and simply put the usercontrol in an iframe and added that to the razor macro.

    Jeroen

  • Jeremy Pyne 106 posts 246 karma points MVP c-trib
    Dec 11, 2012 @ 23:06
    Jeremy Pyne
    1

    This package will let you call one macro from inside another.  Not sure if a user controll would work with postbacks though.  (You would have to make sure that caching is turned off on both macros if there are dynamic display or postbacks.)

     

    https://github.com/tocsoft/Umbraco-Razor-Components

  • CodeMaster2008 151 posts 184 karma points
    Dec 12, 2012 @ 02:52
    CodeMaster2008
    0

    What about razor macros?
    I have a razor macro where I'd like to insert a partial view, I already tried @RenderPage() adn @Html.Partial() with no success.
    What would be the recommended way (if any) to do this? 

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Dec 12, 2012 @ 09:16
    Jeroen Breuer
    0

    You should use the Razor Components package like Jeremy said. Than you can do this to call a Razor macro inside another Razor macro:

    @Library.RenderMacro("MacroAlias", new { stringProp = "value", numberProp= 199, boolProp=true})
    @Library.RenderMacro("~/macroScripts/scriptFile.cshtml", new { Property1 = "value", Prop2= "value2"})

    Jeroen

  • CodeMaster2008 151 posts 184 karma points
    Dec 12, 2012 @ 09:28
    CodeMaster2008
    0

    Is it going to work even if I'm calling a partial view from "/Views/Partials"?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Dec 12, 2012 @ 09:32
    Jeroen Breuer
    0

    Hmm there might be some confusion here. What version of Umbraco are you using? In Umbraco there are 2 versions of Razor. One is the "old" Razor macro which was introduced in 4.7 and one is the "new" Razor MVC which is used when you create a view template in 4.11. All the examples I mentioned are about the "old" Razor macro. I don't know if it will work on "new" Razor MVC.

    Jeroen

  • CodeMaster2008 151 posts 184 karma points
    Dec 12, 2012 @ 09:57
    CodeMaster2008
    0

    I'm using 4.11.1.
    I already have some partial views in "/Views/Partials", which I can render inside my templates (razor templates), using @Html.Partial().
    My idea is to have a macro that will through some logic, "decide" which partial view to render. That's the reason I'm trying to have something similar to @Html.Partial() inside a macro.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Dec 12, 2012 @ 10:10
    Jeroen Breuer
    0

    Why would you use a macro for that? Think it's good to read this blog: http://umbraco.com/follow-us/blog-archive/2012/10/30/getting-started-with-mvc-in-umbraco-410.aspx. The chapter called "The power of the Partial View" is what you need I think.

    Jeroen

  • CodeMaster2008 151 posts 184 karma points
    Dec 12, 2012 @ 10:15
    CodeMaster2008
    0

    The reason is to allow the content editor to insert this functionality (or content) on a given page using TyneMCE and the only way I can think of it is through a macro.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Dec 12, 2012 @ 13:45
    Jeroen Breuer
    0

    For now you can only use the "old" Razor macro in the RTE and I don't know how partial views work in there. In v6 it will be changed: https://groups.google.com/forum/#!msg/umbraco-dev/w4-cnlWDXTk/udLLflab10IJ

    Jeroen

  • CodeMaster2008 151 posts 184 karma points
    Dec 14, 2012 @ 00:37
    CodeMaster2008
    0

    Man, I didn't even know they are already cooking a version 6.
    Thanks for your help. 

  • Psib3r 70 posts 119 karma points
    Jan 17, 2013 @ 12:18
    Psib3r
    0

    I am using the @Library.RenderMacro("~/macroScripts/scriptFile.cshtml",new{Property1="value",Prop2="value2"})

    Now i did this with one macro that uses razor script and it is fine, but I have another macro that has a .net user control behind, it looks like when I use the parameters they are not being picked up. 

     

    It looks like it simply renders the html but does not fire the code behind, I put some debugging in the form_Load and nothing happened.

Please Sign in or register to post replies

Write your reply to:

Draft