Rooney
Rooney

Reputation: 847

How to concatenate two strings in a <img> src tag?

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?

Upvotes: 5

Views: 28224

Answers (4)

Alessandro Cinque
Alessandro Cinque

Reputation: 43

In HTML5 I did:

 <img src="{{basePath  + Data.id +'/' + Data.id + '_0.jpg'}}">

basePath and Data.id are variables and whatever is in between '' like '/' and '_0.jpg' is just what I want to concatenate to those variables.

Upvotes: 0

Prameela B
Prameela B

Reputation: 1

I had a similar subfolder image files accessing issue but below code format worked for my application .

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

Upvotes: -1

webdeveloper
webdeveloper

Reputation: 17288

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"/>

Upvotes: 2

Ant P
Ant P

Reputation: 25221

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"/>

Upvotes: 10

Related Questions