Search code examples
sitecoresitecore8

Sitecore - setting background-image a div to a CMS value


I've been try to add the background image of a div to a value from Sitecore (8.0) in C# MVC using the code

<div style="background-image: url({Model.MyImage.Src})>

Where MyImage is of type Image as returned by GlassView

This is returning html such as

<div style="background-image: url(/~/media/myFolders/myImage.ashx)">

This image isnt being displayed when the page is rendered- although the url resolves when entered into the browser's address bar so it must be an issue with the .ashx extension as a background image for a div.

I also tried using Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem) but this also returned me the ashx which couldn't be resolved!


Solution

  • Try background-image: url('@Model.Image.Src'). While your example doesn't show it, you most likely have spaces in your folder or file name, which requires single or double quotes.