Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 200
Rooney Avatar asked Apr 18 '13 11:04

Rooney


2 Answers

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"/>
like image 153
Ant P Avatar answered Oct 26 '22 16:10

Ant P


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"/>
like image 32
webdeveloper Avatar answered Oct 26 '22 16:10

webdeveloper