I have js DataTable to display list of items. Each item has few properties and few images. I create js DataTable to display all info. Everything is working except the image. Usually image is accessible by link:
@product.ProductImages.ImageUrl
My js DataTable code:
function loadDataTable() {
dataTable = $('#tblData').DataTable({
"ajax": { url: '/admin/product/GetAll' },
"columns": [
{ data: 'name', "width": "15%" },
{ data: 'category.name', "width": "10%" },
{
data: 'imageUrl', "render": function (data, type, row) {
return '<img src="' data.product.ProductImages.FirstOrDefault().ImageUrl + '" "/>';
}, "width": "10%"
},
{ data: 'description', "width": "30%" },
{ data: 'price', "width": "5%" },
}
]
Image is not showing up in the data table. I tried using
<img src="' + data.ProductImages.FirstOrDefault().ImageUrl + '"
or just
<img src="' + data + '">
But nothing works.. I do have code in item controller to display images, and at debugging step it does show up, that each item has N number of images.. I just want to show 1 image in the table..
What am I doing wrong ? Thank you.
================================ Update:
I followed Mr. Farid example, and got Image: null. I guess I am getting image source link incorrectly.
Here is my model :
public class Product
{
public string Name { get; set; }
public Category Category { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public List<ProductImage> ProductImages { get; set; }
}
and
public class ProductImage
{
public int Id { get; set; }
public string ImageUrl { get; set; }
}
So usually I retrieve images with linkL
product.ProductImages.ImageUrl;
Controller code:
public IActionResult Index()
{
List<Product> productList = unitOfWork.Product.GetAll(includeProperties: "Category,ProductImages").ToList();
return View(productList);
}
My JS code:
function loadDataTable() {
$.ajax({
type: "GET",
url: '/admin/product/GetAll',
success: function (response) {
console.log(response);
dataTable = $('#tblData').DataTable({
data: response,
columns: [
{ data: 'name', "width": "15%" },
{ data: 'category.name', "width": "10%" },
{
data: 'imageUrl',
"render": function (data, type, row) {
return '<img src="' + data + '" alt="' + row.name; '" style="height="100% width="100%; "/>';
}, "width": "10%"
},
{ data: 'description', "width": "30%" },
{ data: 'price', "width": "5%" },
]
});
},
error: function (response) {
alert(response.responseText);
}
});
}
This is what I get from browser, I am using microsoft edge. productImages: null.
Few more details:
If I debug controller index method, it shows complete list of products, with pictures and links to them.
link to one of images of the product:

hopefully this helps..
I probably should access image link somehow differently, but I cant figure out how..
Thank you.




Based on your scenario and description, it seems your
data.product.ProductImages.FirstOrDefault().ImageUrlquery might have the problem. Make sure its returning the single and correct image url which you are binding to yourimage srcAs you haven't shared this details, so I couldn't test that. Apart from that, I have investigated with the demo image url and rest of the code and its working as expected.
Here is the steps how I investigated your scenario:
Demo Model:
Demo Cotnroller:
View:
Output: