Get url background image from ImageVault

Vote:
 

Hello,

I have a view that use MainImage as a background. Recenly I have changed type of MainImage from ContentReference to MediaReference, because my client prefer to use the ImageVault. Now I got compile error becuuse MainImage is not type of ContentReference.

 

Does anyone know how to get ImageVault as an url background image? 

Thanks,

/Katharina

#183780
Oct 23, 2017 13:13
Vote:
 

Hi,

Have you tried using something like Html.PropertyFor(x => x.MainImage) to display the image instead? 

#183797
Oct 23, 2017 16:46
Vote:
 

I would recommend creating a Html helper function for this that you can reuse throughout your project(s):

    public static class MediaHelpers
    {
        public static string GetImageVaultMediaUrl<T>(this HtmlHelper<T> html, Expression<Func<T, MediaReference>> expression) {
            var modelMetadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            var mediaReference = modelMetadata.Model as MediaReference;
            var settings = ServiceLocator.Current.GetInstance<PropertySettingsResolver>().GetSettings(typeof(PropertyMediaSettings), modelMetadata.ModelType, modelMetadata.PropertyName) as PropertyMediaSettings;
            var client = ClientFactory.GetSdkClient();
            var media = client.Load<Thumbnail>(mediaReference, settings).FirstOrDefault();
            return media?.Url;
        }
    }

And then call it like this whenever you need the URL:

    <header style="background-image: url('@Html.GetImageVaultMediaUrl(m => m.HeaderImage)');">

Best regards,
Robert
ImageVault

#184995
Edited, Nov 08, 2017 9:52
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.