Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all I am binding the img tag as follows

HTML
<img alt="" src='@Url.Action("UserImage", "Home")' class="avatar img-circle img-thumbnail" /> 


This is my code to get the image from the database

C#
[HttpGet]  
public ActionResult UserImage()  
{  
string user = Session["UserName"] as string;  
byte[] photo = null;  
var v = db.table.Where(p => p.USER_NAME == user).Select(img => img.PHOTO).FirstOrDefault();  
if (v != null)  
{  
photo = v;  
return File(photo, "image/jpeg");  
}  
else  
return null;  
}  


This is displaying image when I have in the database but when it is null it is displaying as follows, instead of that i need to bind src tag with empty

http://i.stack.imgur.com/rFPU0.png[^]

What I have tried:

C#
[HttpGet]  
public ActionResult UserImage()  
{  
string user = Session["UserName"] as string;  
byte[] photo = null;  
var v = db.table.Where(p => p.USER_NAME == user).Select(img => img.PHOTO).FirstOrDefault();  
if (v != null)  
{  
photo = v;  
return File(photo, "image/jpeg");  
}  
else  
return null;  
}  
Posted

You have to return another image for noimage situation. Have a nice photo that show no image available and return it when no image found
 
Share this answer
 
Comments
Krishna Chaitanya 7-Feb-16 6:35am    
That I can do but I would don't want to show any default image
Find a blank image(.jpg) and store it in a folder. When you have null value in your Database then convert the blank image to byte array and send it to client.
C#
byte[] testbyte = System.IO.File.ReadAllBytes("filepath");
if (v != null)  
{  
    photo = testbyte;
    return File(photo, "image/jpeg");
}

Instead of showing wrong image you can show blank image which will represent image is not available.
 
Share this answer
 
v2
Comments
Krishna Chaitanya 7-Feb-16 6:35am    
That I can do but I would don't want to show any default image
John C Rayan 7-Feb-16 14:38pm    
Then check for null and show the image link only when image is not null
[no name] 7-Feb-16 6:44am    
Then what do you want to show when you do not have any data?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900