wilfordrocks
asked on
Display image as part of MVC view.
My goal is to display an image from the database on the SAME view as other information, not in a second browser window.
This method gets an image but returns it to a new browser window.
Controller:
public ActionResult GetImage(
{
MemoryStream ms = GetImageFromDB();
return File(ms.ToArray(), "image/png");
}
If I change the method to store the image in the model, like so
Model:
public FileContentResult imageContent { get; set; }
Controller:
public void GetImage(
{
MemoryStream ms = GetImageFromDB();
alphaIndexModel.imageConte nt = File(ms.ToArray(), "image/png");
}
View:
How can I get it to display in the view? This code does not work.
@if(@Model.imageContent != null)
{
<img src="@Model.imageContent.F ileContent s" style="" height="800" width="1200" />
}
This method gets an image but returns it to a new browser window.
Controller:
public ActionResult GetImage(
{
MemoryStream ms = GetImageFromDB();
return File(ms.ToArray(), "image/png");
}
If I change the method to store the image in the model, like so
Model:
public FileContentResult imageContent { get; set; }
Controller:
public void GetImage(
{
MemoryStream ms = GetImageFromDB();
alphaIndexModel.imageConte
}
View:
How can I get it to display in the view? This code does not work.
@if(@Model.imageContent != null)
{
<img src="@Model.imageContent.F
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So simple. So elegant. Thank you.
I don't understand you choise of answer. CraigWagner only repeated the code from your question.
Just show the image the normal way, let the browser request the image from your `GetImage` resource:
Open in new window
(Adjust the URL to whatever controller name you use.)