c# - 如何在 <img> src 标签中连接两个字符串?

标签 c# html razor asp.net-mvc-4

这里我想在 <img> 中连接两个字符串标签。如何做到这一点??

<img src=" "/partners" + @item.AdPath" alt="" id="adimg" title="@item.AdName"  width:"50px" height="50px"/>

有什么建议吗?

最佳答案

你应该能够做到这一点:

<img src="/partners+@(item.AdPath)" alt="" id="adimg"
    title="@item.AdName"  width:"50px" height="50px"/>

Razor 引擎会将 @item.AdPath 替换为实际值,为您提供 src="/partners+[value]"

由于 Razor 表达式是唯一被解析的内容,因此您不必尝试将字符串连接逻辑添加到标记中 - 只需将 Razor 表达式放入您希望值出现的位置即可。

编辑:或者,如果您不想要加号(您的评论中不清楚):

<img src="/partners@(item.AdPath)" alt="" id="adimg"
    title="@item.AdName"  width:"50px" height="50px"/>

或者,您可以尝试使用 String.Format:

<img src="@String.Format("/partners{0}", item.AdPath)" alt="" id="adimg"
    title="@item.AdName"  width:"50px" height="50px"/>

关于c# - 如何在 <img> src 标签中连接两个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16081150/

相关文章:

c# - 当索引为 ( array.length - 1 ) 时,发生 IndexOutOfRangeException,但代码仍然执行

html - CSS3 :not selector for div and its children

asp.net-mvc-3 - MVC 3 Razor,具有自定义标记/部分的助手

c# - 自定义 map 路线

C# 类属性可枚举

c# - Databound CheckBoxList 中的第一项未显示

c# - 如何在两组数据中找到部分不同的项目子集?

html - 没有javascript的CSS菜单

css - div样式继承问题

javascript - Accordion 内的 jQuery 可选