如何使用 if/else 语句在 c# 中对图像进行硬编码
本文关键字:图像 编码 if 何使用 else 语句 | 更新日期: 2023-09-27 18:33:57
我目前正在制作一个网站,我需要一些帮助。我有 10 个需要具有不同图像的框,并希望使用 If/else 语句来做到这一点。
a href="@Url.Action("View","Entry", new { id = i })" target="_blank"><img class="test" src="~/Content/pictures/test.jpg" alt="" style="width:30%;height:25%;"/>
这就是我实现盒子的方式,上面有一个 for 循环。我现在想让它们每个人都有不同的图像,但我在使用我的 if 语句时出现错误:
if (i == 1)//where i is the ID of the image
{
i["src"] = ("~/Content/pictures/test.jpg");
}
我使用这个时遇到错误,因为我是一个 int 并且它需要一个字符串。尝试将 i 转换为字符串,但这对八人没有帮助。这可能不是正确的方法,所以请分享你对此的看法。谢谢你的时间。
听起来你在这里真正想要的东西可能很简单:
<img class="test" src="@(imagePaths[i])" ...
其中imagePaths
是所需路径的string[]
(假设从 0 开始)。当然,你可以把它包装在一个函数中:
@functions
{
public string GetImagePath(int index) {
switch(index) {
case 1: return "whatever.png";
case 2: return "something.png";
default: return "oops.png";
}
}
}
...
<img class="test" src="@GetImagePath(i)" ...