mvc @ Url.Content .......图像不显示 [英] mvc @Url.Content.......images not displaying

查看:107
本文介绍了mvc @ Url.Content .......图像不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mvc EF6 从SQL Server数据库显示图像,

I am using mvc EF6 to display images from SQL server database,

我们已将图像保存到本地文件夹,并将其URL保存到数据库.

We have saved the image to the local Folder, and its URL to the Database.

做得好

图片网址另存为

C:\ Users \ Naveed \ Desktop \ EndToEnd \ UserImages \ 6360831428333175151-posterior-teeth.jpg

C:\Users\Naveed\Desktop\EndToEnd\UserImages\6360831428333175151-posterior-teeth.jpg

然后我在视图中使用以下代码显示图片.

then i use below code in my view to display the Images.

@model IEnumerable<iSmileFinal.Models.Imaging>
@if (Model != null)
{
<div class="adsm-sec">
    @foreach (var vad in Model)
    {
        <div id="img">
            <div class="adsm-image-wraper">
                @*@{string imagePath = Url.Content("~/UserImages/");}*@

                @*<img src='@Url.Content("~/UserImages/")' alt="no photo" />*@

                <img src='@Url.Content(vad.ImageUrl)' alt="no photo" class="adsm-image" />

                <img src='@Url.Content(String.Format(vad.ImageUrl))' style="width:200px; height:90px;" />
            </div>
        </div>

       <img>@Html.DisplayFor(modelItem => vad.ImageUrl)

        @Html.DisplayFor(modelItem => vad.ImageType)

        @Html.DisplayFor(modelItem => vad.Comments)
    }
</div>

}

,但未显示图像.它显示为(参见图片)

but it is not displaying the images. it displays like (See Image)

我使用Google Chrome和FF.

i use Google Chrome and FF.

推荐答案

C:\ Users \ Naveed \ Desktop \ EndToEnd \ UserImages \ 6360831428333175151-posterior-teeth.jpg

C:\Users\Naveed\Desktop\EndToEnd\UserImages\6360831428333175151-posterior-teeth.jpg

这不是图像 url

这是文件的物理路径.上载图像时,不应将其存储在数据库中.相反,您应该存储相对于Web应用程序的url/唯一文件标识符.

This is a physical path to the file. You should not be storing this in your database when uploading an image. Instead you should be storing the url/unique file identifier relative to your web app.

类似于 yourSite/UserImages/uniqueFileName.jpg 或只是 uniqueFileName.jpg"

现在,呈现页面时,请从db表中读取此值,并将此相对URL用作图像 src 属性.如果仅存储文件名,则可能需要将其附加到存储文件位置"UserImages \" + yourFileNameVariable; 并使用该文件名使其相对.

Now when you render the page, read this value from your db table and use this relative url as the image src property. If you are simply storing the filename, you probably need to append it to the storage file location "UserImages\"+yourFileNameVariable; and use that to make it relative

如果 UserImages 目录位于应用程序的Web根目录中,则可以执行此操作

If youe UserImages directory is in the web root of your application, you can do this

<img src="@Url.Content("~/UserImages/yourFileName.png")"/>

如果要在db表中存储文件名,请用c#变量/model属性名替换 yourFileName.png .

If you are storing file name in your db table, replace yourFileName.png with your c# variable /model property name.

例如,如果您的模型/视图模型具有名为 ImageName 的属性,该属性具有唯一的文件名(仅文件名),您可以这样做

For example, If your model/viewmodel has a property called ImageNamewhich has the unique file name(just the file name), you can do like this

<img  src="@Url.Content("~")/Uploads/@Model.ImageName"/>

@ Url.Content(〜")将返回到Web根目录的相对路径

@Url.Content("~") will return the relative path to the web root

编辑:根据评论.

显示所有项目.您可以在循环中移动此图片标签.

To show all items. you can move this image tag inside your loop.

@model IEnumerable<iSmileFinal.Models.Imaging>
@if (Model != null)
{
<div class="adsm-sec">
    @foreach (var vad in Model)
    {
        <img src="@Url.Content("~")/Uploads/@vad.ImageName" />
    }
 </div>
}

假设Imaging类具有 ImageName 属性,您将在其中加载存储在〜/Uploads 目录下的文件名.

Assuming Imaging class has an ImageName property in which you will load the file name stored under ~/Uploads directory.

这篇关于mvc @ Url.Content .......图像不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆