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.

6
  • @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? Commented Dec 3, 2015 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 Commented Dec 3, 2015 at 11:53
  • What error? If you pass the correct url, it just needs to be <img scr=" + item.url + " style="...." /> where item.url might be say "~/Images/myimage.png" Commented Dec 3, 2015 at 11:57
  • @StephenMuecke okk i am going to use url but i will definitly get some error let me try Commented Dec 3, 2015 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("<img src= + item. />"); but its not working should i post a new question ?? Commented Dec 3, 2015 at 12:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.