panjo
panjo

Reputation: 3515

render image on the razor page

I'm using asp.net mvc3 to upload images to web server. Problem is in dev mode when displaying images on razor view I have following to render images

@foreach (var image in Model.Photos)
{                    
   <img src="@image.Path" />                    
}

On page source everything seems fine, html tags are rendered like

<img src="C:\Projects\MySite\Content\uploads\Jellyfish.jpg" />
<img src="C:\Projects\MySite\Content\uploads\Lighthouse.jpg" />

On disk drive location image is correctly uploaded so everything seems ok but photo is not rendered.

Any thoughts?

Thanks

Upvotes: 2

Views: 4466

Answers (2)

codingbiz
codingbiz

Reputation: 26376

The value of @image.Path should be relative to your website and not the full path to your hard-drive. From your model, the Path property should read

/Content/uploads/Jellyfish.jpg 

or simply

/Jellyfish.jpg

and should be generated like this in your view

<img src="@Url.Content("~/Content/Uploads/@image.Path")" />

Upvotes: 1

Magnus Karlsson
Magnus Karlsson

Reputation: 3579

Your path should correspond to your website, not your hard drive. The correct path would be: "/Images/Jellyfish.jpg". So upload it to a folder in your project and you should be fine!

Here is a complete example: Displaying an uploaded image in MVC 3 Razor

Upvotes: 0

Related Questions