0

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.

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.