Image display on MVC view

帅比萌擦擦* 提交于 2020-07-16 16:45:25

问题


I am trying to display image on the cshtml file. The spam filter prevents me from typing the HTML for image. However the source is set to

src= "@Html.Encode(Model.PictureLocation)"  alt="IMAGES"

In viewsource it shows as

src= "C:\Documents and Settings\xxx\My Documents\Visual Studio 2010\Projects\MVC\TIQC_ServerInterface\TIQC_ServerInterface\uploads\FileUpload12011_03_02_11_49_22.jpg"  alt="IMAGES"

The image is present in the location mentioned in the src path.

On execution, the page is not displaying the images. Let us know if anything wrong here?


回答1:


You have specified an absolute path which doesn't exist on the client computer. Try like this:

<img src= "@Url.Content("~/uploads/FileUpload12011_03_02_11_49_22.jpg")" alt="IMAGES" />

or if your model variable contains "~/uploads/FileUpload12011_03_02_11_49_22.jpg" you could:

<img src= "@Url.Content(Model.PictureLocation)" alt="IMAGES" />



回答2:


The path in the src attribute must be relative to the website root, not the absolute path on the server. So in your case that would probably be something like "/uploads/FileUpload12011_03_02_11_49_22.jpg".



来源:https://stackoverflow.com/questions/5248273/image-display-on-mvc-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!