Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to keep the image path value in a variable and pass it to img src property, but its not working.

Below is my code...

HTML
<div class="js-container-assortmentlist">
  @{
    string layoutImage1 = "FORMULE1UNTIL3_MOBIELETAFEL1_A_SIDE.jpg";
    string layoutImage2 = "FORMULE1UNTIL3_MOBIELETAFEL1_B_SIDE.jpg";
    string imageSource1 = "~/images/TafelLayouts/" + layoutImage1;
    string imageSource2 = "~/images/TafelLayouts/" + layoutImage2;    
  }

  <div style="width:30%; margin-left: auto;margin-right: auto;display:block " id="layoutImage1">
    <img class="" id="imgLayout" src="@imageSource1" alt="@layoutImage1">
  </div>
  <div style="width:30%; margin-left: auto;margin-right: auto;display:none " id="layoutImage2">
    <img class="" id="imgLayout" src="@imageSource2" alt="@layoutImage2">
  </div>
  <br />
</div>


What I have tried:

When I am providing the value for img src directly like "~/images/TafelLayouts/FORMULE1UNTIL3_MOBIELETAFEL1_A_SIDE.jpg" itsworking fine.

As i need to load dynamic images , so I need to pass this value dynamically.
Posted
Updated 18-Jul-19 7:19am
Comments
MadMyche 18-Jul-19 9:49am    
What does the rendered HTML look like? What does the browsers debugger show for these images on the network tab?
ZurdoDev 18-Jul-19 12:51pm    
What he said. ^

1 solution

Hi,
The problem is: If you put the full variable as an attribute value, you should resolve the full URL yourself in imageSource1 and imageSource2.

Else, you can do the workaround (note that "~/" is moved to attribute value):
JavaScript
<div class="js-container-assortmentlist">
  @{
    string layoutImage1 = "FORMULE1UNTIL3_MOBIELETAFEL1_A_SIDE.jpg";
    string layoutImage2 = "FORMULE1UNTIL3_MOBIELETAFEL1_B_SIDE.jpg";
    string imageSource1 = "images/TafelLayouts/" + layoutImage1;
    string imageSource2 = "images/TafelLayouts/" + layoutImage2;    
  }

  <div style="width:30%; margin-left: auto;margin-right: auto;display:block " id="layoutImage1">
    <img class="" id="imgLayout" src="~/@imageSource1" alt="@layoutImage1">
  </div>
  <div style="width:30%; margin-left: auto;margin-right: auto;display:none " id="layoutImage2">
    <img class="" id="imgLayout" src="~/@imageSource2" alt="@layoutImage2">
  </div>
  <br />
</div>
 
Share this answer
 
v2
Comments
Nityananda Das 19-Jul-19 0:19am    
Thanks a lot Anurag...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900