jaabh
jaabh

Reputation: 1004

ASP.NET CORE displaying image set to instance of a property

I have a property

public class Robusta
{
    public int Id {get;set;}
    public int CoffeeId {get;set;}
    public string Name {get;set;}
    public string Origin {get;set;}
    public string ImagePath {get;set;}
}

I have ImagePath set as ~/images/imageName.jpg in the database. ~/images/imageName.jpg works and displays image in the view if I do <img src="~/images/imageName.jpg" /> so I thought that if I set property to that string by making ImagePath='~/images/imageName.jpg' and in my View I displayed

<td>
    <img src="@Html.DisplayFor(modelItem => item.ImagePath)" />
</td>

that it would work, but it does not display the image. How can I add the image as a property and display it? Because I have different images for every instance.

Upvotes: 0

Views: 73

Answers (1)

Fei Han
Fei Han

Reputation: 27793

How can I add the image as a property and display it?

You can use Url.Content method to resolve a url for your Image file when you pass it the relative path, like below.

<td>
    @*<img src="@Html.DisplayFor(modelItem => item.ImagePath)" />*@
    <img src="@Url.Content(item.ImagePath)" />
</td>

Upvotes: 1

Related Questions