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
    Feb 09, 2022 @ 22:21
    Mike Manusama
    0

    Find a value on a page that has been picked with content picker

    I am migrating over a card element that worked fine on Umbraco 7, but am having trouble in Umbraco 9. Essentially I have a Macro for a card that uses the content picker to select a URL. Once I have the URL, I grab the featured image from that page and use it as the image for the card. I was able to get access to this value in Umbraco 7 with no issues, but I have given this a run for the better half of a week now and I cannot find a suitable solution. Below is the code with a few different cracks at it (commented out). I can't seem to get back the URL for the featured image of the page that is being selected though. Can anyone figure out what the issue might be? It is much appreciated.

    @inherits Umbraco.Cms.Web.Common.Macros.PartialViewMacroPage
    
    @{
        var linkTo = Umbraco.Content(Model.MacroParameters["cardIntURL"]);
    
        @* This attempt got Error loading partial view *@
        @* var getFeaturedImg = Umbraco.TypedMedia(linkTo.GetPropertyValue("featuredImage")).Url; *@
    
        @* This attempt got Error loading partial view *@
        @* var getFeaturedImg = Umbraco.Media(linkTo.GetPropertyValue("featuredImage")); *@   
    
        @* Card renders with no value for variable *@
        @* var getFeaturedImg = Umbraco.Media(linkTo.Value("featuredImage"));   *@
    
    
        @* Card renders with the value being "Umbraco.Cms.Core.Models.MediaWithCrops`1[Umbraco.Cms.Infrastructure.PublishedCache.PublishedContent]" *@
        @* var getFeaturedImg = linkTo.Value("featuredImage"); *@
    
        @* Card renders with no value for variable *@
        var getFeaturedImg = Umbraco.Media(linkTo.HasValue("featuredImage"));
    
    }
    
    
    
    @if (linkTo!=null){
    
        @* Passing variables in p tags just to see what gets returned *@
        <p>@linkTo.Url()</p>
        <p>@getFeaturedImg</p>
    
    
    
        <div class="cds-card-blog cds-rounded cds-shadow-hover">
            <img class="cds-card-blog-img-top img-fluid card-img-top w-100 d-block" src="/media/oo0bsuxx/staffing-trends-2022.jpg">
            <div class="cds-card-blog-body">
                <a class="cds-card-blog-header" href="@linkTo.Url()">
                    <h3 class="cds-text-journey cds-mt-0">@Html.Raw( @Model.MacroParameters["cardIntTitle"] )</h3>
                </a>
                <p>@Html.Raw( @Model.MacroParameters["cardIntContent"] )</p>
                <a class="cds-card-blog-link" href="@linkTo.Url()">@Model.MacroParameters["cardIntBtnText"]</a>
            </div>
        </div>
    
    }
    
  • Mike Manusama 45 posts 195 karma points
    Feb 14, 2022 @ 16:19
    Mike Manusama
    100

    For anybody else running into this issue - I was able to figure it out using the code below:

    @inherits Umbraco.Cms.Web.Common.Macros.PartialViewMacroPage
    
    
    @{
        var linkTo = Umbraco.Content(Model.MacroParameters["cardIntURL"]);
        @* Get URL path for featured image *@
        var getFeaturedImg = ( ((IPublishedContent)linkTo.Value("featuredImage")).Url() );
    }
    
    
    
    @if (linkTo!=null){
    
        <div class="cds-card-blog cds-rounded cds-shadow-hover">
            <img class="cds-card-blog-img-top img-fluid card-img-top w-100 d-block" src="@getFeaturedImg">
            <div class="cds-card-blog-body">
                <a class="cds-card-blog-header" href="@linkTo.Url()">
                    <h3 class="cds-text-journey cds-mt-0">@Html.Raw( @Model.MacroParameters["cardIntTitle"] )</h3>
                </a>
                <p>@Html.Raw( @Model.MacroParameters["cardIntContent"] )</p>
                <a class="cds-card-blog-link" href="@linkTo.Url()">@Model.MacroParameters["cardIntBtnText"]</a>
            </div>
        </div>
    
    
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft