简体   繁体   中英

ASP.NET MVC with ENTITY FRAMEWORK (SQL SERVER DATABASE) Image not showing in view ... The error says 'cannot convert "byte" to "string"'

I am new to ASP.NET MVC so please don't judge me... i am having problem where the image from my sqlserver(byte data type) is not showing to my view.It says cannot convert byte to string. What should i do?**

* THIS is my Controller named ViewProduct



    public ActionResult ViewProduct()
        {
            
            return View();
        }

        public ActionResult ViewProd()
        {

            inventoryDBEntities1 dbe = new inventoryDBEntities1();
            return View(dbe.tbl_product.ToList());
        }

THIS Is my Model named tbl_product



     public partial class tbl_product
    {
        public int productID { get; set; }
        public byte[] prod_image { get; set; }
       
    }

THIS is my view page


   @model IEnumerable<PointofSale.Models.tbl_product>


<table>
    <tr>
        <td>
            Image
        </td>
    </tr>


    <tr>

        @foreach (var item in @Model)
        {
            <td>
                 //The error is arround here( V )!!
                <img src="@Url.Content(item.prod_image)" height="100" width="100"/>
            </td>
        }

    

    </tr>



</table>

You need to display the image with byte[] by converting it as base64 string .

@foreach (var item in @Model)
{
   <td>
       @{
           string imageBase64Data = Convert.ToBase64String(item.prod_image);
           string imageDataURL = string.Format("data:image/png;base64,{0}", imageBase64Data);

           <img src="@Url.Content(imageDataURL)" height="100" width="100" />
        }
   </td>
}

References

Display Image From Byte Array In ASP.NET MVC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM