I have an Image folder with some files, and a table with the path of this images. When trying to list the images like this:
@foreach (var image in Model.Product.Image)
{
<img src="@image.Path" height="100" />
}
ASP.NET references @image.Path with the Page route(print from Google Dev Tools):
It seems to be really simple to solve this problem, but I’ve got no clue how to do.
I just need to remove the Products/ part o f the URL and my problem is solved. I don’t understand why Razor references @image.Path in this context with the name of the Controller.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Try somthing like this
@foreach (var image in Model.Product.Image)
{
var iPhotoUrl = "/Image/" + (image.Path ?? "noimage.png");
<img src="@iPhotoUrl" height="100" />
}
Also share the value return on @image.Path
Method 2
For my delight, just putting an ~/ before the @image.Path(I think it “hardcoded” the source) solved the problem.
The code:
<img src="~/@image.Path" height="100" />
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
