Asp.NET MVC 5显示来自数据库的图像

时间:2017-05-06 17:18:09

标签: asp.net-mvc database asp.net-mvc-5

我试图从我的数据库中显示图片。 首先,我在模型中创建了一个名为ShopModels.cs的模型我有这个(我觉得现在很好):

    public class ShopModels
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Photo { get; set; }
        public decimal Price { get; set; }
    }
}

在我制作了这个模型之后,我已经将我的控制器和视图搭建了脚手架 在我的控制器中,我没有为图像更改任何其他东西。 对于我的View(查看文件夹名为:ShopModels),所谓的Detials.cshtml

在我的Detials.cshtml中,我改变了这个:

@Html.DisplayFor(model => model.Photo)

到此:

<img src="@Url.Content(@Model.Photo)" />

它仍然不起作用,也不会给我一个错误。

我的数据库看起来像这样: 我试过尝试改变

  

图片/ JohnSmith.png

  

〜/图片/ JohnSmith.png

但仍然没有运气

enter image description here

树:

enter image description here

这是我得到的结果: enter image description here

正如你所看到的那样,我得到了文字而不是图像。

2 个答案:

答案 0 :(得分:0)

我认为你应该改变:

@Html.DisplayFor(model => model.Photo)

到此:

<img src="@Url.Content(@Model.Photo)" />

在Index.cshtml或Shop.cshtml中

通过你的照片看起来有一张桌子,大多数是那些文件之一

答案 1 :(得分:0)

您可以通过此代码显示图像

<img src="data:image/*;base64,@(Convert.ToBase64String(Model.Photo))">
相关问题