我的asp.net MVC视图中有以下代码: -
<a href="~/Content/uploads/@item.ID.ToString()" + ".png"><img class="thumbnailimag" src="~/Content/uploads/@item.ID.ToString()" + ".png" /></a>
但我无法将.png
连接到我的href & src
。有人可以建议吗?
感谢
答案 0 :(得分:1)
您的报价未正确关闭。
href='@string.Format("~/Content/uploads/{0}.png", item.ID)'
完整代码
<a href='@string.Format("~/Content/uploads/{0}.png", item.ID)'><img class="thumbnailimag" src='@string.Format("~/Content/uploads/{0}.png", item.ID)' /></a>
答案 1 :(得分:1)
您需要将代码包装在括号中,如explained here。无需再拨打.ToString()
:
href="~/Content/uploads/@(item.ID).png"
答案 2 :(得分:1)
或者,在href之外声明fileName
(IMO使其更具可读性)
@{
var fileName = item.ID.ToString() + ".png";
}
<a href="~/Content/uploads/@fileName"><img class="thumbnailimag" src="~/Content/uploads/@fileName" /></a>