Copied to clipboard

Flag this post as spam?

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


  • Mike Manusama 45 posts 195 karma points
    Jul 27, 2018 @ 14:29
    Mike Manusama
    0

    Dynamic URL for link using Partial View Macro with Content Picker Parameter

    I am currently using the Content Picker parameter in Umbraco 7.4.1. Like a previous issue I had, I was able to do this on U7.10, but using that same method didnt get me those results on this current instance. I tried 3 different ways and searched multiple forums for an answer and I can't seem to get it to work.

    Attempt 1 (Works on U7.10, but doesn't load on 7.4):

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Core.Models
    @using Umbraco.Web
    @using Umbraco.Web.Extensions
    
    
     @{
      IPublishedContent linkedNode = Udi.Parse(Model.MacroParameters["myParameter"].ToString()).ToPublishedContent();
    }
    
    <a href="@linkedNode.Url" class="">button text</a> 
    

    Attempt 2 (Error on 7.4):

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Core.Models
    @using Umbraco.Web
    @using Umbraco.Web.Extensions
    
    
    
    @{
        IPublishedContent test = Umbraco.TypedContent((int)Model.MacroParameters["myParameter"]);
    }
    
    
    <a href="@test.Url" class="">button text</a> 
    

    Attempt 3:

    <p>@Model.MacroParameters["myParameter"]</p>
    

    This one renders the macro, but only pulls in the node ID number associated with that page on my site.

    Any idea why this wouldn't be working? Thanks in advance!

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jul 27, 2018 @ 19:08
    Dan Diplo
    0

    What is @Model.MacroParameters["myParameter"] returning?

    Is it an integer (1234) or a UDI (which looks something like umb://document/a3fc5438670444f38ddcc34adbaa44a7) ?

  • Mike Manusama 45 posts 195 karma points
    Jul 30, 2018 @ 12:32
    Mike Manusama
    0

    the 4 digit integer

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 30, 2018 @ 14:23
    Alex Skrypnyk
    0

    Hi Mike

    If the node with 4 digits integer exists this code should work on 7.4:

    @{
        IPublishedContent test = Umbraco.TypedContent((int)Model.MacroParameters["myParameter"]);
    }
    

    Thanks,

    Alex

  • Mike Manusama 45 posts 195 karma points
    Jul 30, 2018 @ 14:39
    Mike Manusama
    0

    Hi Alex,

    Thank you. How do I reference this in the html though?

    @{
                IPublishedContent test = Umbraco.TypedContent((int)Model.MacroParameters["greyMediaUrl"]);
                <p>@Model.MacroParameters["greyMediaUrl"]</p>
            }
    

    That renders an error. As does this:

    @{
                    IPublishedContent test = Umbraco.TypedContent((int)Model.MacroParameters["greyMediaUrl"]);
                    <p>@test.Url</p>
                }
    

    Do I need to reference anything at the top of the Partial Macro as well?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 30, 2018 @ 14:40
    Alex Skrypnyk
    0

    Model.MacroParameters["greyMediaUrl"] - is this media Id?

    If it's media Id you have to use Umbraco.TypedMedia method:

    @{
                    IPublishedContent test = Umbraco.TypedMedia((int)Model.MacroParameters["greyMediaUrl"]);
                    <p>@test.Url</p>
                }
    
  • Mike Manusama 45 posts 195 karma points
    Jul 30, 2018 @ 14:46
    Mike Manusama
    0

    This is for a content picker, not media. "greyMedia" was just the alias I gave my parameter.

    The proposed solution did not do the trick either. I am wondering what the issue could be. as a reference, here is the rest of the partial view macro if it helps.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
        <div class="row ltds-media ltds-media--gray ltds-p-md">   
            <div class="col-sm-3 col-xs-4">
    
               @{
                    var mediaGrayImgMain = Model.MacroParameters["greyMediaImg"];
                    var mediaItemOne = Umbraco.TypedMedia(mediaGrayImgMain);
                    <img src=' @mediaItemOne.Url ' class="media-object img-responsive ltds-media_img" alt=' @Model.MacroParameters["greyMediaHeader"] '>
                }
    
            </div>
    
           <div class="col-sm-9 col-xs-8 ltds-media_body">
                <h3 class="ltds-media_heading">@Model.MacroParameters["greyMediaHeader"]</h3>
                <p class="ltds-media_text">@Model.MacroParameters["greyMediaText"]</p>
    
    
                @{
                    IPublishedContent test = Umbraco.TypedMedia((int)Model.MacroParameters["greyMediaUrl"]);
                    <p>@test.Url</p>
                }
    
    
            </div>
        </div>
    

    Here is a screenshot of the parameters: enter image description here

    All of the other parameters are working except for this one

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 30, 2018 @ 20:53
    Alex Skrypnyk
    0

    What is the error here:

    @{
                IPublishedContent test = Umbraco.TypedContent((int)Model.MacroParameters["greyMediaUrl"]);
                <p>@Model.MacroParameters["greyMediaUrl"]</p>
            }
    
  • Mike Manusama 45 posts 195 karma points
    Jul 30, 2018 @ 20:55
    Mike Manusama
    0

    enter image description here

    I just get a generic error like this

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 30, 2018 @ 20:56
    Alex Skrypnyk
    0

    Can you have a look at Umbraco log? What is the error there?

  • Mike Manusama 45 posts 195 karma points
    Jul 30, 2018 @ 21:07
    Mike Manusama
    0

    2018-07-30 17:02:27,341 [P5068/D2/T101] WARN umbraco.macro - Error loading Partial View (file: ~/Views/MacroPartials/Grey Media Module.cshtml). Exception: System.InvalidCastException: Specified cast is not valid. at ASP.PageViewsMacroPartialsGreyMediaModule_cshtml.Execute() in mysite.com\Views\MacroPartials\Grey Media Module.cshtml:line 20 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult) at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content) at umbraco.macro.LoadPartialViewMacro(MacroModel macro) at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 31, 2018 @ 08:56
    Alex Skrypnyk
    0

    Hi Mike

    Try this code:

        @{
            IPublishedContent test = Umbraco.TypedContent(Model.MacroParameters["greyMediaUrl"]);
            if (test != null)
            {
                <p>@test.Url</p>
            }
        }
    
  • Mike Manusama 45 posts 195 karma points
    Jul 31, 2018 @ 14:29
    Mike Manusama
    0

    Well this is quite the annoying issue! It still doesnt work. Here's what I have (for your reference):

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
        <div class="row ltds-media ltds-media--gray ltds-p-md">   
            <div class="col-sm-3 col-xs-4">
    
               @{
                    var mediaGrayImgMain = Model.MacroParameters["greyMediaImg"];
                    var mediaItemOne = Umbraco.TypedMedia(mediaGrayImgMain);
                    <img src=' @mediaItemOne.Url ' class="media-object img-responsive ltds-media_img" alt=' @Model.MacroParameters["greyMediaHeader"] '>
                }
    
            </div>
    
           <div class="col-sm-9 col-xs-8 ltds-media_body">
                <h3 class="ltds-media_heading">@Model.MacroParameters["greyMediaHeader"]</h3>
                <p class="ltds-media_text">@Model.MacroParameters["greyMediaText"]</p>
    
            @{
                IPublishedContent test = Umbraco.TypedMedia(Model.MacroParameters["greyMediaUrl"]);
                if (test != null) {
                    <p>@test.Url</p>
                }
            }
    
            </div>
        </div>
    

    The Macro in Umbraco looks like this: enter image description here

    And on my page, it looks like this: enter image description here

    And on the frontend it renders everything EXCEPT the URL:

    enter image description here

    Anything look out of the ordinary? I am very appreciative of your help on this!

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 31, 2018 @ 14:32
    Alex Skrypnyk
    101

    Try this:

    @{
                IPublishedContent test = Umbraco.TypedContent(Model.MacroParameters["greyMediaUrl"]);
                if (test != null) {
                    <p>@test.Url</p>
                }
            }
    

    TypedContent instead of TypedMedia

  • Mike Manusama 45 posts 195 karma points
    Jul 31, 2018 @ 14:47
    Mike Manusama
    0

    I think this worked. Thank you! I am not sure how resolving the issue works on the forum. Since this last one fixed it, you can edit your original post and I will mark that as correct! thanks again!

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 31, 2018 @ 15:03
    Alex Skrypnyk
    0

    Great, really cool that it's fixed :) Mark, please, as solved latest comment, I also edited the previous one. Have a great evening.

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft