MultiURL Picker - Rendering the link on the fronte...
# help-with-umbraco
w
I have a MultiURL Picker and I want to render the link URL or the Anchor. This is what I have in my file so far;
Copy code
@using Umbraco.Core.Models.Blocks
@inherits UmbracoViewPage<BlockListItem<HomepageBanner>>

<div class="homepage-heading block" id="block_1">
    <div class="inner">
        <div class="text-part">
            <h1>@Model.Content.BannerHeading</h1>
            @Model.Content.BannerContent
            <p><a class="button arrow down" href="@Model.Content.BannerButtonLink"><span>Find</span> <span>out</span> <span>more</span> <span></span><span class="down"></span></a></p>
        </div>
        <div class="image-part">
            <img width="425" height="427" src="https://principle-networks.com/wp-content/uploads/2022/04/Hands-Small-e1650969882279.jpg" class="attachment-full size-full" alt="image" loading="lazy" />                    
        </div>
    </div>
</div>
I also need to render out the image that has been selected in the CMS. These are my fields for the block: https://prnt.sc/Ujj9pmdUdZzw Thanks, Javed
a
Hi. For the link, this depends on whether you have set the limit in the data type to
1
. This controls whether the picker works a a single picker or a multi picker - where the return values are either
Link
or
IEnumerable<Link>
respectively. Assuming you have configurd it as a single picker, I think your HTML for the link could look something like:
Copy code
cshtml
@if (Model.Content.BannerButtonLink is not null) {
    <p><a class="button arrow down" href="@Model.Content.BannerButtonLink.Url" target="@Model.Content.BannerButtonLink.Target"><span>Find</span> <span>out</span> <span>more</span> <span></span><span class="down"></span></a></p>
}
w
These are the settings that I see - I don't see where I need to set the limit. https://prnt.sc/5klfMO94ECdW - it'll always be one. Aha! I see where to set the limit - doh! I forgot that bit!
I'm getting error CS1503: Argument 1: cannot convert from 'method group' to 'object'
a
Can you share a screenshot of the data type?
w
Sorted it 🙂 I just need to figure how to render my image now.
a
Can you see what type your
BannerImage
property is?
w
Yes, Image Media Picker
It'll always be a single image too
I've tried
@Model.Content.BannerImage.Url
Ah - it's
MediaUrl
and not
Url
- there is the mistake.
So what would the Alt text be?
@Model.Content.BannerImage.Alt
?
a
That depends on a few things. Can you try writing out
@Model.Content.BannerImage
somewhere on the page to see what it says?
w
Umbraco.Core.Models.MediaWithCrops`1[Umbraco.Web.PublishedModels.Image]
a
Ok. So it wraps the
Image
type. If you've added a property with the alias
altText
to the image type, you should be able to access it via
Model.Content.BannerButtonLink.Content.AltText
w
Ah - I understand now, so I thought in the media section we could upload an image and then set the alt text from there, but it looks like you can't do that. I will add an altText property and use that instead 🙂
Thanks for the support on this, much appreciated. A great learning curve this. The last time I used Umbraco was Umbraco 6.
a
You can add an
altText
property or similar directly to the image media type, but users won't be able to set the alt text until after the media has been uploaded
s
For alt text, one of the things I like to do is create the property for the media file (as suggested), and then supplement this with an extension method.
Copy code
csharp
public static string AltText(this MediaWithCrops content)
{
    if(content.HasProperty("altText") && !string.IsNullOrWhiteSpace(content.Value<string>("altText")))
    {
        return content?.Value<string>("altText") ?? "";
    }
        
    return content?.Name ?? "";
}
It means that there's always a fallback option available when you're outputting it in razor.
w
That's a solid approach! I like it. I'll implement this in my project 🙂
I forgot to ask you yesterday, where would you put this code in the project?
s
Normally, I'd have a second project with core files in it, something like "YourProjectName.Core", then have a folder for Extensions and then name the file after the type we're adding to the extension. So an example of this would be - YourProjectName.Core > Extensions > MediaWithCropsExtensions.cs And then in the file...
Copy code
csharp
namespace YourProjectName.Core.Extensions
{
    public static class MediaWithCropsExtensions
    {
        public static string AltText(this MediaWithCrops content)
        {
            if(content.HasProperty("altText") && !string.IsNullOrWhiteSpace(content.Value<string>("altText")))
            {
                return content?.Value<string>("altText") ?? "";
            }

            return content?.Name ?? "";
        }
    }
}
However, if you don't have a second project, you can just put the Extensions folder in your main project and update the namespaces accordingly. To then use this in your razor file, add the namespace to the usings list -
@using YourProjectName.Core.Extensions;
And to call the item in your code (using your BannerImage property as the example)
@Model.Content.BannerImage.Image.AltText()
.
7 Views