Home > Enterprise >  Is it possible to use property in the src path
Is it possible to use property in the src path

Time:12-03

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

At the moment my code is:

<img src="../Pandora.jpg"  alt="@store.Name Logo">

but I want something like:

<img src="../@store.Name.jpg"  alt="@store.Name Logo">

CodePudding user response:

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"  alt="@store.Name Logo">

Demo @ .NET Fiddle

CodePudding user response:

It is not possible to use a property in the src path like that. You would need to use a string concatenation or interpolation syntax to combine the property with the rest of the src path.

For example, using string concatenation:

<img src="../'   @store.Name   '.jpg"  alt="@store.Name Logo">

Or using interpolation syntax:

<img src="../${@store.Name}.jpg"  alt="@store.Name Logo">

You may also need to ensure that the property value is properly formatted for use in a file path, such as removing any spaces or special characters.

  • Related