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"/>
Share:
24,370
Rooney
Author by

Rooney

Updated on November 05, 2021

Comments

  • Rooney
    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
      Nolonar about 11 years
      I see many strings inside your <img> tag. Which ones do you need to concatenate? Or in other words: What output do you expect?
    • Rooney
      Rooney about 11 years
      i want to concatenate /partners with @item.AdPath
    • Rooney
      Rooney about 11 years
      output should be like /partners+adpathvalues
    • RajeshKdev
      RajeshKdev about 11 years
      Have you tried by using Server Tags <%= item.AdPath %>
    • René Wolferink
      René Wolferink about 11 years
      @RJK: He's using razor, not asp, so the @ is the tag indicating server-side logic.
  • Rooney
    Rooney about 11 years
    If i give it like this /[email protected] getting like this /[email protected] not the @item.AdPath value
  • Rooney
    Rooney about 11 years
    i need /partners string with @item.AdPath value
  • Ant P
    Ant P about 11 years
    Odd, 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
    René Wolferink about 11 years
    The 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
    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
    Rooney about 11 years
    Hi if i want to concatenate item.AdPath + item.Status means what should i do??
  • Ant P
    Ant P about 11 years
    If you're using String.Format - src="@String.Format("/partners{0}{1}", item.AdPath, item.Status)"