Copied to clipboard

Flag this post as spam?

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


  • BEWD 71 posts 233 karma points
    Sep 16, 2019 @ 13:02
    BEWD
    0

    Content Picker in Macro Parameter - Can't link to page

    Hi

    I am using Umbraco 8.1.4 and I am having a little problem with Macro Parameters.

    Basically I am trying to use a content picker in a macro parameter and then use the url of the page selected as a link to that page.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    <a href="@Model.MacroParameters["linkTo"]" role="button">
        @Model.MacroParameters["buttonText"]
    </a>
    

    The button renders fine but the URL doesn’t work. It just shows the UDI (umb://document/…….)

    I have searched around and found various solutions for previous Umbraco versions but they don’t seem to work in v8.

    Is someone able to point me in the right direction for v8? I can get content pickers to work properly in a normal template just not as macro parameter?

    Thanks

    Ben

  • Marc Goodson 1158 posts 7744 karma points MVP 5x c-trib
    Sep 16, 2019 @ 15:36
    Marc Goodson
    100

    Hi Ben

    When you write out a content picker value normally in a template then a 'Property Value Converter' runs behind the scenes and turns the picked identifier into a representation of the thing that was picked.

    But inside a Macro Partial, and with Macro Parameters this magic does not happen :-( so what you are seeing for linkTo is the identifier of the picked content item...

    ... but you can use this to get your item with the UmbracoHelper's Content method... eg try

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
    var linkTo = Umbraco.Content(Model.MacroParameters["linkTo"]);
    }
    @if (linkTo!=null){
    <a href="@linkTo.Url()" role="button">
        @Model.MacroParameters["buttonText"]
    </a>
    }
    

    regards

    Marc

  • BEWD 71 posts 233 karma points
    Sep 16, 2019 @ 15:46
    BEWD
    0

    Perfect thanks Marc. I was very close with some of the attempts but was just struggling with the .Url()

    That code works a treat and makes complete sense. Thanks mate.

Please Sign in or register to post replies

Write your reply to:

Draft