James Lalor
James Lalor

Reputation: 1246

Access image URL in Umbraco 8

I've been trying to utilise Umbraco 8 in a new project. (I've got C# experience but never used Umbraco before)

I've added templates and set it up to accept data, however I've set up a "Home" DocType and added an Image Selector. After setting and publishing the data, I'm now trying to update the template to display the Image, and keep running into errors, I've tried:

var backgroundImageId = Model.Value("backgroundImage");
var backgroundImage = Umbraco.Media(backgroundImageId);
@Model.Value("backgroundImage").Url

And a bunch of others which haven't panned out. I just can't seem to get it to work.

Any suggestions appreciated.

Upvotes: 0

Views: 1284

Answers (2)

Rushabh Master
Rushabh Master

Reputation: 490

There are several ways you can get this.

IPublishedContent contentbackgroundImage = umbracoHelper.TypedContent(backgroundImageId);

var backgroundImage = contentbackgroundImage?.GetPropertyValue<IPublishedContent>("backgroundImage");

you can do like this way on the razor pages

@{
    var backgroundImage = Model.GetValue<IPublishedContent>("backgroundImage")
}

from this you can access properies like url, name ,alies ect.

Upvotes: 0

James Lalor
James Lalor

Reputation: 1246

var backgroundImage = Model.Value<IPublishedContent>("backgroundImage").Url;

Fouund the solution to this, converting the type to published content seems to do the trick.

Upvotes: 4

Related Questions