How to concatenate two strings in a <img> src tag?
24,370
Solution 1
You should simply be able to do this:
<img src="/partners+@(item.AdPath)" alt="" id="adimg"
title="@item.AdName" width:"50px" height="50px"/>
The Razor engine will replace @item.AdPath
with the actual value, giving you src="/partners+[value]"
.
Since the Razor expression is the only thing that's parsed, you don't have to try and work string concatenation logic into the tag - simply drop in the Razor expression where you want the value to appear.
Edit: Or, if you don't want the plus sign (not clear from your comments):
<img src="/partners@(item.AdPath)" alt="" id="adimg"
title="@item.AdName" width:"50px" height="50px"/>
Alternatively, you could try with String.Format
:
<img src="@String.Format("/partners{0}", item.AdPath)" alt="" id="adimg"
title="@item.AdName" width:"50px" height="50px"/>
Solution 2
It could be done like this:
First:
<img src="@("/partners" + item.AdPath)" alt="" id="adimg" title="@item.AdName" width:"50px" height="50px"/>
Second:
<img src="/partners@(item.AdPath)" alt="" id="adimg" title="@item.AdName" width:"50px" height="50px"/>
Author by
Rooney
Updated on November 05, 2021Comments
-
Rooney over 2 years
Here I want to concatenate two strings inside a
<img>
tag. How to do this??<img src=" "/partners" + @item.AdPath" alt="" id="adimg" title="@item.AdName" width:"50px" height="50px"/>
Any suggestion?
-
Nolonar about 11 yearsI see many strings inside your
<img>
tag. Which ones do you need to concatenate? Or in other words: What output do you expect? -
Rooney about 11 yearsi want to concatenate /partners with @item.AdPath
-
Rooney about 11 yearsoutput should be like /partners+adpathvalues
-
RajeshKdev about 11 yearsHave you tried by using Server Tags
<%= item.AdPath %>
-
René Wolferink about 11 years@RJK: He's using razor, not asp, so the
@
is the tag indicating server-side logic.
-
-
Rooney about 11 yearsIf i give it like this /[email protected] getting like this /[email protected] not the @item.AdPath value
-
Rooney about 11 yearsi need /partners string with @item.AdPath value
-
Ant P about 11 yearsOdd, that should work. Are your other Razor expressions parsed? Try my last update using
String.Format
or wrapping the Razor expressions in brackets. -
René Wolferink about 11 yearsThe reason
/[email protected]
doens't work is that razor uses intelligent recognintion for email adresses. It has evaluated that this is an email address and should not be interpreted as a razor expression, which is why that one comes out wrong. So int hat case the@(abc.xyz)
syntax can be used, making razor understand it's not an email address. -
Ant P about 11 years@RenéWolferink Interesting! Did not know that. I suppose it's sensible to disambiguate with parentheses in these scenarios, regardless.
-
Rooney about 11 yearsHi if i want to concatenate item.AdPath + item.Status means what should i do??
-
Ant P about 11 yearsIf you're using
String.Format
-src="@String.Format("/partners{0}{1}", item.AdPath, item.Status)"