Reputation: 1627
I have a database that is storing an image in it as a byte array and the corresponding class in EF. Now I have a view that is strongly typed taking in an IEnumerable. In the view I want to loop through all of the MyImage's that come in and display them, something like:
foreach(var drawing in model)
{
<img [email protected] alt="drawing" />
}
This is the simplified version of what I want. What do I need to do instead of that img tag???
Upvotes: 2
Views: 577
Reputation:
Try to create some Action Method that return bytes array as Content as image/png Content-type.
class ImageContent
{
public ActionMethod Image(int? id = 0)
{
// Get byte from table by image id
....
return Content(byteArray.ToString(), "image/png");
}
}
and pass URL to this Action Method
@foreach(var drawing in Model)
{
<img src="@Url.Content("~/ImageContent/Image/" + drawing.DrawingID)" />
}
Upvotes: 0
Reputation: 1854
I suppose you need to create the separate controller for returning images. This controller will take image id (or whatever id) and return the image how described for example here Render image to the screen from MVC controller
So, your code would look like:
foreach(var drawing in model)
{
<img src="@Url.Action("GetImage", "Image", new {id=drawing.ImageId})" alt="drawing" />
}
The second approach is not to return image but firstly to save them onto disc and return the path to it. But probably it's not what you want.
Upvotes: 5