Copied to clipboard

Flag this post as spam?

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


  • Jimmy 31 posts 147 karma points
    Apr 19, 2016 @ 16:42
    Jimmy
    0

    get macro parameter in .net usercontrol

    Hi, I am new to Umbraco.

    We have some legacy .net code, which we would like to use as macros. I have created the macro, and added a few parameters, and the macro can be inserted in RTE.

    My question is, how do i retrieve the macro parameter value in my .net code. My parameter alias i called "dec2" for this test.

    I would think that it is something like this, but no luck

    var val = Umbraco.Model.MacroParameters["dec2"];
    

    or

    var val = Umbraco.TypedContent(Model.Macroparameters["dec2"]);
    

    Hope someone can guide in a direction :-)

    Best regards, and thanks

    Jimmy

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Apr 19, 2016 @ 18:16
    Dennis Adolfi
    0

    Hi Jimmy.

    Have you tried:

    var dec2 = Model.MacroParameters["dec2"].ToString();

  • Jimmy 31 posts 147 karma points
    Apr 20, 2016 @ 04:59
    Jimmy
    1

    Hi Dennis

    Thanks, it looks right, but still does not work, I get a

    "Error loading userControl '~/usercontrols/test.ascx' 1"

    Maybe my problem is that I am not sure how to use Model, and how to get a instance of it.

    Should Model just be available, or do I need to import something?

    Sorry, but I am quite new to Umbraco :-)

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Apr 20, 2016 @ 06:21
    Dennis Adolfi
    0

    Hi again Jimmy.

    What version of Umbraco are you using?

    The Model object is accessible if your view inhertits Umbraco.Web.Macros.PartialViewMacroPage.

    Here is a complete code for the macro partial view:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var dec2 = Model.MacroParameters["dec2"].ToString();
    }
    
    <p>@dec2</p>
    

    https://our.umbraco.org/documentation/reference/templating/macros/Partial-View-Macros/

    If this still does´nt work, check your logfile. The error you pasted is just a generic error message macros usally exposes to avoid exposing sensitive code. Your /Config/Logs/ file should have more info about the error.

    Lets me know if this works for you!

    Great to hear that you just started using Umbraco! Welcome to a great community!

  • Jimmy 31 posts 147 karma points
    Apr 20, 2016 @ 07:48
    Jimmy
    0

    Thanks :-)

    I am using Umbraco version 7.4.2 assembly: 1.0.5921.28477

    I am not using Razor script, but plain old .net code :-)

    I have looked at the log file, and I guess the the first problem is that the conrol property "dec2" "doesn't exist or aren't accessible (public)"

     2016-04-20 09:37:23,479 [P13192/D5/T427] INFO  umbraco.macro - Usercontrol added with id 'test_1'
     2016-04-20 09:37:23,479 [P13192/D5/T427] INFO  umbraco.macro - Complete (took 0ms)
     2016-04-20 09:37:23,479 [P13192/D5/T427] INFO  umbraco.macro - Warning: control property 'dec2' doesn't exist or aren't accessible (public)
     2016-04-20 09:37:23,479 [P13192/D5/T427] INFO  umbraco.macro - Complete (took 0ms)
    
  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Apr 20, 2016 @ 08:08
    Dennis Adolfi
    100

    Sorry, i missed the fact that you are building a usercontrol and not a MacroPartialView. I never work with Usercontrols so im not much help sorry.

    See this tutorial on how to use macros within a usercontrol and adding macro parameters: http://www.nibble.be/screencasts/Addingcustomusercontrols.htm

    Or read this thread: https://our.umbraco.org/forum/developers/extending-umbraco/26153-Use-parameters-of-macro-with-usercontrol

    Your macro parameter simply maps to a public property of your user control. It's a bit more convenient to start from adding a property to the control since umbraco has a feature of determining such properties and offers to add corresponding macro parameters automatically.

  • Jimmy 31 posts 147 karma points
    Apr 20, 2016 @ 08:51
    Jimmy
    1

    Thank you so much, that worked

    So simple :-)

    Next time you are in Copenhagen I will buy you a beer and a pizza :-)

    Best regards

    Jimmy

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Apr 20, 2016 @ 08:56
    Dennis Adolfi
    0

    Haha i´ll hold you to that! Going to Codegarden this summer! :)

    Great to hear that it worked out for you, happy i could help. Seeing that i dont usally do UserControls (usally prefer MacroPartialViews), this was quite educational for me to. So we both learned something here! :)

    Have a great day!!

  • Jimmy 31 posts 147 karma points
    Apr 20, 2016 @ 09:06
    Jimmy
    1

    Yes do that. If you come by Copenhagen, give me a shout :-)

    Have a great day too!!

Please Sign in or register to post replies

Write your reply to:

Draft