user1482281
user1482281

Reputation: 25

Show image from DB

Sorry for this question. There is a lot of information but unfortunately I haven't found the answer. Could you please help me? I have a main view

Details View:

@model Shops.ShopModel
......
@Html.Partial("_PictureViewer", Model.Picures)
......

And in main view include PartialView

_PictureViewer View:

@model List<Model.PictureOriginalModel>
.....
@foreach (var p in Model)
{ 
  <img src='@new File(@p.Trunk, "image/jpeg");' alt='' />
}
.....

PictureOriginalModel.cs: namespace Model {

  public class PictureOriginalModel
 {
    [DataType(DataType.Text)]
    [Display(Name = "File Name.")]
    public string FileName { get; set; }

    [Display(Name = "Trunk.")]
    public Image Trunk { get; set; }

    [DataType(DataType.Text)]
    [Display(Name = "Description.")]
    public string Description { get; set; }
  }
}

And in result I have an error: error CS1031: Type expected Could you please clarify how to show image correct?

Upvotes: 0

Views: 340

Answers (4)

Nazar Tereshkovych
Nazar Tereshkovych

Reputation: 1472

[HttpGet]
public FileResult GetImage(int evaluatorId, int employeeId)
{
    byte[] imgByteArr = this._employeeManager.GetEmployeePhoto(employeeId);
    return imgByteArr != null ? File(imgByteArr, "image/png") : null;
}

Upvotes: 0

Govind Malviya
Govind Malviya

Reputation: 13743

You need to create action for returning image type response

Controller code

public class HomeController
{
    public ActionResult Image( int id )
    {
        var imageData = getImagebyte(); // your function to return image byte

        return File(imageData, "image/jpg");
    }
}

on your view call of this Action

<img src='@Url.Action("Image","Home", new { id = 1 })' />

Upvotes: 1

Rajesh Subramanian
Rajesh Subramanian

Reputation: 6490

You can do as follows,

public FileContentResult GetImage(...) 
{
  //do somethings
   return File(image, mimeType);
}

<img src="<%= Url.Action("GetImage", "PictureOriginalModel", new { Id = someId}) %>" />

Upvotes: 1

Sanja Melnichuk
Sanja Melnichuk

Reputation: 3505

You need to create controller action which return your image, something like:

[HttpGet]
public ActionResult GetImage(long id, ...) 
{
  ...
   return File(fileBytes, "image/png");
}

And in client code just set correct link to your controller GetImage method

Upvotes: 0

Related Questions