Mohammadov
Mohammadov

Reputation: 613

how to display image from database to razor view?

so i have images in my database and i want to show them in my view,this is my model:

public class SocialNetworkModel
{
    [Required]
    [DataType(DataType.Text)]
    public string titlePost { get; set; }

    [Required]
    [DataType(DataType.Text)]
    public string descriptionPost { get; set; }

    public byte[] picturePost { get; set; }
 }

this is my view :

foreach (var item in Model) {
<tr id="[email protected]">
    <td>
        @Html.DisplayFor(modelItem => item.titlePost)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.descriptionPost)
    </td>
    <td>
        //here i want to put my image
    </td>
</tr>
}

so please if someone has any idea i will be very grateful.

Upvotes: 0

Views: 2270

Answers (1)

Guanxi
Guanxi

Reputation: 3131

Create action in your controller as below

public FileContentResult displayImage(int productId) {
    SocialNetwork item = //Get your object
    if (item!= null) {
        return File(item.picturePost, "image Mime Type");
    } else {
    return null;
 }

}

And show this returned file in the view.

Upvotes: 1

Related Questions