ASP.NET 根据正确的 ID 显示图像

huangapple go评论80阅读模式
英文:

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。表格包含ImageIDImagePath,数据看起来像这样:'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,但没有成功。

我会感激任何对此的帮助! ASP.NET 根据正确的 ID 显示图像

英文:

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 =&gt; c.CategoryID == id),
            Categories = categoriesService.GetCategories(),
    	    Images = imageService.GetImages(),
	        ProductImages = new Dictionary&lt;int, string&gt;()
    };

    foreach (var product in model.Products)
    {
	    var image = model.Images.FirstOrDefault(img =&gt; 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&lt;Images&gt;? Images { get; set; }
public Dictionary&lt;int, string&gt; 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&lt;int, string&gt; 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];
	  &lt;img src=&quot;@imagePath&quot; alt=&quot;Product Image&quot;/&gt;
    }
}

When I replace the src with src=&quot;images/filename.png&quot; 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! ASP.NET 根据正确的 ID 显示图像

答案1

得分: 1

请复制以下内容:&lt;img src=&quot;@imagePath&quot; alt=&quot;Product Image&quot;/&gt;

英文:

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 = $&quot;/{Model.ProductImages[item.ProductID]}&quot;;
       &lt;img src=&quot;@imagePath&quot; alt=&quot;Product Image&quot;/&gt;
    }
}

Please copy here &lt;img src=&quot;@imagePath&quot; alt=&quot;Product Image&quot;/&gt;, from source code of your browser, after page generated.

huangapple
  • 本文由 发表于 2023年7月7日 03:57:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定