Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic background-image

I want to have multiple div with different background URLs. My inline razor for this code seems to be wrong:

<table>
@foreach (var item in fa.get_albums()) {
<tr>
    <td>
        <div style="background-image:url('@item.picture');">
            ///something
        </div>        
    </td>          
</tr>
}
</table>

What's the right way to put inline razor in to background-imag:url()?

like image 933
n.shojaei Avatar asked Jul 15 '15 10:07

n.shojaei


People also ask

How do I make my background image dynamic?

Navigate to Structure > Pages. Edit the page where you want to display a background image. In the left navigation menu, click on General. At the bottom of the page, unravel the fieldset called Dynamic Background.

How do I download a background picture?

To download a background image, right-click the background image you want to download and click Save background as.... A Save Picture dialog box will appear, allowing you to select the directory in which you want to save the image.


1 Answers

The issue you have is that MVC will happily fix the relative paths inside an img src attribute but not for style. You should map that virtual path using Url.Content():

<div style="background-image:url('@Url.Content(item.picture)');">
like image 156
DavidG Avatar answered Nov 15 '22 07:11

DavidG