n.shojaei n.shojaei - 1 year ago 120
CSS Question

Dynamic background-image

I want to have multiple

with different background URLs.
My inline razor for this code seems to be wrong:

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

What's the right way to put inline razor in to

Answer Source

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)');">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download