0

This is my JavaScript function where I am able to pull data from action method in JSON format:

   <script type="text/javascript">
         $(document).ready(function () {
         $.getJSON("/Home/GetUsers", null, function (data) {
         var div = $('#ajaxDiv');
         div.html("<br /> " + "Users received from server: " + "<br />");
         $.each(data, function (i, item)
         {
          printUser(div, item);
            });
          });
        });
     function printUser(div, item)
     {
      div.append("<br/>" + "UserName: " + item.UserName + "<br/>" + "Id: " + item.Id);
      <img src="@Url.Action("GetFileData", "Home", new { id = item.Id })" style="width:100px;height:100px;"/>
     }      
  </script>

As you can see, I am able to get item.Id, but how do I send it into src tag new { id = item.Id } It is showing syntax error. I just want to show the image within this Ajax div. Please help me out.

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
duke
  • 1,816
  • 2
  • 18
  • 32
  • `@Url.Action()` is razor code which is parsed on the server before its sent to the view. `item` is a javascript variable which does not even exist at that point (its not in scope). You need to manually build the url. But why not just return the file path in the json data? –  Dec 03 '15 at 11:48
  • okk i will manually build the url path and pass it into json data but then i will get error at how to display it onto view page @StephenMuecke – duke Dec 03 '15 at 11:53
  • What error? If you pass the correct url, it just needs to be `` where `item.url` might be say `"~/Images/myimage.png"` –  Dec 03 '15 at 11:57
  • @StephenMuecke okk i am going to use url but i will definitly get some error let me try – duke Dec 03 '15 at 12:03
  • @StephenMuecke I have got the url but how to pass in img src attribute I have tried something like this: div.append(""); but its not working should i post a new question ?? – duke Dec 03 '15 at 12:25
  • Yes you need to post a new question. –  Dec 03 '15 at 12:26

0 Answers0