英文:
ASP.NET Displaying image acording to the correct ID
问题
我正在处理一个学校项目,尝试在我的ASP.NET应用程序中为每个产品显示一张图片。然而,我一整天都在解决这个问题,但似乎找不到解决办法。
在我的控制器中,我有以下代码来加载每个类别的所有产品和图像数据:
public IActionResult GetProducts(int id)
{
var model = new ShoppingCartModel
{
Products = service.GetProducts().Where(c => c.CategoryID == id),
Categories = categoriesService.GetCategories(),
Images = imageService.GetImages(),
ProductImages = new Dictionary<int, string>()
};
foreach (var product in model.Products)
{
var image = model.Images.FirstOrDefault(img => img.ImageID == product.ImageID);
if (image != null)
{
model.ProductImages.Add(product.ProductID, image.ImagePath);
}
}
return View(model);
}
我将所有数据从数据库加载到Images
中,它是一个IEnumerable
。表格包含ImageID
和ImagePath
,数据看起来像这样:'1' | 'images/filename.png'。每个产品都有一个与之关联的ImageID
:
public IEnumerable<Images>? Images { get; set; }
public Dictionary<int, string> ProductImages { get; set; }
所有文件都位于我的ASP.NET项目的wwwroot
文件夹中,所以wwwroot/images/filename.png
。我的模型还有一个ProductImages
,它是一个Dictionary<int, string>
,我将使用它来找到产品的正确图像路径。
然后,我在foreach
中遍历所有产品,并为每个产品找到正确的图像并将它们添加到字典中。
然后在我的视图中有这样的代码:
@foreach (var item in Model.Products)
{
@if (Model.ProductImages.ContainsKey(item.ProductID))
{
var imagePath = Model.ProductImages[item.ProductID];
<img src="@imagePath" alt="Product Image"/>
}
}
当我将src
替换为src="images/filename.png"
时,它确实可以工作。我已经调试过,查看了imagePath
是什么,它是正确的imagePath
。我不知道为什么它不起作用。我也尝试使用JavaScript来做,然后将路径传回src
,但没有成功。
我会感激任何对此的帮助!
英文:
I'm working on a school project and I'm trying to display a image for each product in my ASP.NET application. However I've been on this issue the whole day and I can't seem to find a fix.
In my controller I have this code for loading all the products for each category and image data:
public IActionResult GetProducts(int id)
{
var model = new ShoppingCartModel
{
Products = service.GetProducts().Where(c => c.CategoryID == id),
Categories = categoriesService.GetCategories(),
Images = imageService.GetImages(),
ProductImages = new Dictionary<int, string>()
};
foreach (var product in model.Products)
{
var image = model.Images.FirstOrDefault(img => img.ImageID == product.ImageID);
if (image != null)
{
model.ProductImages.Add(product.ProductID, image.ImagePath);
}
}
return View(model);
}
I load all the data from the database into the Images
which is a IEnumerable
. The table contains a ImageID
and a ImagePath
data looks like this '1' | 'images/filename.png'. Each product has a ImageID
linked to it:
public IEnumerable<Images>? Images { get; set; }
public Dictionary<int, string> ProductImages { get; set; }
All files are in my wwwroot
folder in my ASP.NET project. So wwwroot/images/filename.png
. My model also has a ProductImages
which is a Dictionary<int, string>
which I will use to find the correct imagePath for the product.
I then loop in all the products in the foreach
, and find the correct image for each product and add them to the dictionary.
Then I have this in my view:
@foreach (var item in Model.Products)
{
@if (Model.ProductImages.ContainsKey(item.ProductID))
{
var imagePath = Model.ProductImages[item.ProductID];
<img src="@imagePath" alt="Product Image"/>
}
}
When I replace the src
with src="images/filename.png"
it does work. I already debugged to see what imagePath
was and it's the correct imagePath
. I have no idea why it's not working. I've also tried doing it with JavaScript and then passing the path back into the src
but no luck.
I'd appreciate any help with this!
答案1
得分: 1
请复制以下内容:<img src="@imagePath" alt="Product Image"/>
。
英文:
It may depend on where your View is. Try to specify a relative path not from the current directory, but from the root directory. The path must start with /, for example: "/images/filename.jpg"
@foreach (var item in Model.Products)
{
@if (Model.ProductImages.ContainsKey(item.ProductID))
{
var imagePath = $"/{Model.ProductImages[item.ProductID]}";
<img src="@imagePath" alt="Product Image"/>
}
}
Please copy here <img src="@imagePath" alt="Product Image"/>
, from source code of your browser, after page generated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论