在asp.net mvc 5中从数据库中获取字节数据中的图像

时间:2017-05-04 06:36:49

标签: c# asp.net razor asp.net-mvc-5

我正在尝试以二进制格式显示存储在sql数据库中的图像。我正在使用剃刀语法通过将其格式更改为base64来检索图像。成功检索字节数据,但从不显示为图片格式。下面是我到目前为止尝试过的代码。谢谢!

酒店介绍

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Travel.Context;

namespace Travel.Models.Hotel
{
    public class HotelInfo
    {
        private int _hotelid;
        private string _hotelname;
        private string _hoteldesc;
        private string _hotelprice;
        private byte[] _hotelpicture;
        //private HttpPostedFileBase _UploadedFile;



        public HotelInfo()
        {
            this._hotelid = 0;
            this._hotelname = string.Empty;
            this._hoteldesc = string.Empty;
            this._hotelprice = string.Empty;

        }

        [Key]
        public int Hotelid
        {
            get
            {
                return _hotelid;
            }

            set
            {
                _hotelid = value;
            }
        }

        public string Hotelname
        {
            get
            {
                return _hotelname;
            }

            set
            {
                _hotelname = value;
            }
        }

        public string Hoteldesc
        {
            get
            {
                return _hoteldesc;
            }

            set
            {
                _hoteldesc = value;
            }
        }

        public string Hotelprice
        {
            get
            {
                return _hotelprice;
            }

            set
            {
                _hotelprice = value;
            }
        }


        public byte[]  Hotelpicture
        {
            get
            {
                return _hotelpicture;
            }

            set
            {
                _hotelpicture = value;
            }
        }


    }
}

HotelController.cs

public ActionResult HotelDescription()
        {
            return View(db.Hotels.ToList());
        }

HotelDescription.cshtml

@model IEnumerable<Travel.Models.Hotel.HotelInfo>

@{
    ViewBag.Title = "HotelDescription";
    Layout = "~/Views/Shared/_Header.cshtml";
}

<h2>HotelDescription</h2>

<p>
    @Html.ActionLink("Create New", "CreateHotel")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Hotelname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Hoteldesc)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Hotelprice)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Hotelpicture)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Hotelname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Hoteldesc)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Hotelprice)
        </td>
        <td>
            @{
                var base64 = Convert.ToBase64String(item.Hotelpicture);
                var imagesrc = string.Format("data:image/jpeg;base64,{0}", base64);

            }
           <img src = "imagesrc" style = 'max-height:100px;max-width:100px' />

        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Hotelid }) |
            @Html.ActionLink("Details", "Details", new { id=item.Hotelid }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Hotelid })
        </td>
    </tr>
}

    @section Scripts {

    }

</table>

1 个答案:

答案 0 :(得分:1)

使用Razor C#语法 - 内联表达式(变量和函数)以@。

开头

因此,如果你编辑

<img src = "imagesrc" style = 'max-height:100px;max-width:100px' />

<img src = "@imagesrc" style = 'max-height:100px;max-width:100px' />

您的代码应该可以成功运行。