Reputation: 1
I am new to ASP.NET MVC so please don't judge me... I am having a problem where the image from my SQL Server (byte data type) is not showing to my view. It says "Cannot convert the byte[]
to string". What should I do?
This is my controller method ViewProduct
:
public ActionResult ViewProduct()
{
return View();
}
public ActionResult ViewProd()
{
inventoryDBEntities1 dbe = new inventoryDBEntities1();
return View(dbe.tbl_product.ToList());
}
This is my model class named tbl_product
:
public partial class tbl_product
{
public int productID { get; set; }
public byte[] prod_image { get; set; }
}
And this is my view:
@model IEnumerable<PointofSale.Models.tbl_product>
<table>
<tr>
<td>
Image
</td>
</tr>
<tr>
@foreach (var item in @Model)
{
<td>
// The error is around here ( V )!!
<img src="@Url.Content(item.prod_image)" height="100" width="100"/>
</td>
}
</tr>
</table>
Upvotes: 0
Views: 134
Reputation: 51450
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>
}
Display Image From Byte Array In ASP.NET MVC
Upvotes: 1