webjaved
07/14/2023, 10:47 AM@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,
JavedAnders Bjerner
07/14/2023, 12:04 PM1
. 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:
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>
}
webjaved
07/14/2023, 12:21 PMwebjaved
07/14/2023, 12:24 PMAnders Bjerner
07/14/2023, 12:28 PMwebjaved
07/14/2023, 12:29 PMAnders Bjerner
07/14/2023, 12:32 PMBannerImage
property is?webjaved
07/14/2023, 12:37 PMwebjaved
07/14/2023, 12:37 PMwebjaved
07/14/2023, 12:38 PM@Model.Content.BannerImage.Url
webjaved
07/14/2023, 12:45 PMMediaUrl
and not Url
- there is the mistake.webjaved
07/14/2023, 12:48 PM@Model.Content.BannerImage.Alt
?Anders Bjerner
07/14/2023, 12:52 PM@Model.Content.BannerImage
somewhere on the page to see what it says?webjaved
07/14/2023, 12:54 PMAnders Bjerner
07/14/2023, 12:57 PMImage
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
webjaved
07/14/2023, 12:59 PMwebjaved
07/14/2023, 1:04 PMAnders Bjerner
07/14/2023, 1:05 PMaltText
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 uploadedStewart
07/17/2023, 3:59 PMcsharp
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.webjaved
07/17/2023, 4:00 PMwebjaved
07/18/2023, 10:14 AMStewart
07/18/2023, 10:20 AMcsharp
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()
.