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.
@Url.Action()is razor code which is parsed on the server before its sent to the view.itemis 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?<img scr=" + item.url + " style="...." />whereitem.urlmight be say"~/Images/myimage.png"