Search code examples
c#razor

Is it possible to use property in the src path


I would like to use @store.Name in the src="../Pandora.jpg".

At the moment my code is:

<img src="../Pandora.jpg" class="card-img-top" alt="@store.Name Logo">

but I want something like:

<img src="../@store.Name.jpg" class="card-img-top" alt="@store.Name Logo">

Solution

  • It is possible with Razor syntax. Make sure that you need to use @() to append the string and avoid .jpg treat as the nested property.

    <img src="../@(store.Name).jpg" class="card-img-top" alt="@store.Name Logo">
    

    Demo @ .NET Fiddle