I am new to Razor syntax in ASP.MVC4 and need to format a string within a loop to provide a run time path to the correct image file.
I am having problems in formatting the string within the For Each list item. Can you have a look at the below and let me know what I’m missing.
<ul class="column">
@For Each item As modeltype In Model
@<li>
@Code
dim itemImage as string = String.Format("../../Images/{0}.png", @item.MajorAssembly)
End Code
<a href="#"><img src="@itemImage"/></a>
</li>
Next
</ul>
Take the @ off of @item.MajorAssembly. When you are inside an @Code block, everything is considered code and additional @’s are unnecessary.
Also, if you want, you could put the String.Format call directly into the src like this: