Quantcast
Channel: C1 CMS Foundation - Open Source on .NET
Viewing all articles
Browse latest Browse all 2540

New Post: How to use IImageFile in a Razor function with background-image url

$
0
0
Trying to build a Razor Function that will allow the user to select an image and it will be become the background-image url of a div tag. However, when the page renders I'm not getting what I expected.
@inherits RazorFunction

@functions {
    public override string FunctionDescription
    {
        get { return "Testing Images"; }
    }

    [FunctionParameter(Label = "Background Image", Help = "Image to use for the background")]
    public DataReference<IImageFile> Image { get; set; }
}

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://www.composite.net/ns/function/1.0">
<head>
</head>
<body>
    <div style="background-image: url(/media(@Image));"></div>
    <img src="/media(@Image)" style="float:right" />
</body>
</html>
The <img> tag works nicely and produces a valid url to image; however, the background-image:url results in an invalid URL.
<div style="background-image: url(/media(MediaArchive:aec60a01-84b5-4699-9a37-47730b911d81));"></div>

<img src="/media/aec60a01-84b5-4699-9a37-47730b911d81/bX4oNw/Testing/FooBar.png" style="float:right">
I also tried the MediaUrl, but it too only works correctly in the context of an Img tag src attribute.
<div style="background-image: url(@Html.C1().MediaUrl(@Image));"></div>
    <img src="@Html.C1().MediaUrl(@Image)" />

    <div>@Html.C1().MediaUrl(@Image).ToHtmlString()</div>
How do you make this work?

Thank you

Viewing all articles
Browse latest Browse all 2540

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>