tags:

views:

44

answers:

1

How can I create a dynamic url to display an image?

I am creating a web page that lists search results of products and their associated images.

I tried using Url.Content, but it does not format the url correctly.

Code I created:

<img src="../../Images/<%: product.PicFileName %>" alt="photo" />

Html that was output:

<img src=".. ..="" Images="" nopic.jpg="" alt="photo" ="">

I also tried creating a helper method but it created the exact same output:

public static string GetPicUrl(string picFileName)
    {
        string picUrl = "../../" + picFileName;

        return picUrl;
    }
+1  A: 

Just tried this:

<img src="<%: Url.Content("/Images/" + filename) %>" alt="foo" />

where filename = "foo.jpg" and it gives me <img src="/Images/foo.jpg" alt="foo"/> which links to http://hostname/Images/foo.jpg

Please let us know what format you want the Url in if this isn't what you had in mind and I will try to help you some more.

ridecar2
That worked great, thanks! That was a clever way of embedding a model value into the helper method.
Castielle