1

I am trying to add attach my variable on my script section, since my filename is in GUID format. The filename is in hidden field:

 string strDirectory = Server.MapPath(Url.Content("~/Content/AnnouncementImages/"));
 string[] strFiles = Directory.GetFiles(strDirectory);
 string strFileName = string.Empty;
      foreach (var strFile in strFiles)
       {
          strFileName = Path.GetFileName(strFile);
       }
 <img id="myImg" src="@Url.Content("~/Content/AnnouncementImages/" + strFileName)" width="300" height="200" />    
 <input type="hidden" id="hiddenStringFileName" value="@strFileName"/>

In script section, I am trying to get this hidden field value:

function fncAnnouncementLoad() 
{
   var filename = document.getElementById('hiddenStringFileName');

   //This is not working so far since it says cannot resolve symbol filename on below code:
   modalImg.src = '@Url.Content("~/Content/AnnouncementImages/" + filename);
}
6
  • @Url.Content() is razor code and is evaluated on the server before its sent to the view. filename is a javascript variable which does not even exist at that point. Commented Nov 28, 2016 at 2:30
  • @StephenMuecke Thanks for that, that make sense. Do you have idea on how can I pass My whole URL Content plus the file name on my javascript section? Commented Nov 28, 2016 at 2:31
  • @WillyDavidJr It will work. You are probably debugging this on the server side and therefore you seem to think that. Debug this on the client side and it will work. However, filename will just contain the dom element and not what you think. Commented Nov 28, 2016 at 2:33
  • Not really clear what you trying to do and why you need a hidden input (or what modalImg is), but @Url.Content("~/Content/AnnouncementImages") + '/' + filename Commented Nov 28, 2016 at 2:35
  • 1
    Note - would also need to be var filename = document.getElementById('hiddenStringFileName').value; Commented Nov 28, 2016 at 2:54

1 Answer 1

1

I am not sure what you are trying to do but if you want razor code plus javascript, you can put it in your view. Obviously this will no longer be unobtrusive javascript. But to do what you want, this will work. I added a button when you click it, it calls the function to show the concatenated URL.

@{
    ViewBag.Title = "Test";
    var strFileName = Guid.NewGuid();
}
<input type="hidden" id="hiddenStringFileName" value="@strFileName" />
<button onclick="fncAnnouncementLoad()">Show</button>
<script>
function fncAnnouncementLoad()
{
   var filename = document.getElementById('hiddenStringFileName').value;

   //This is not working so far since it says cannot resolve symbol filename on below code:
   var src = '@Url.Content("~/Content/AnnouncementImages/")' + filename;
    alert(src);
}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

It works perfectly fine now. Thank you for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.