Asp.net mvc 4 Image binding MVC actionresult razor view

Asp.net mvc 4 Image binding MVC actionresult razor view,asp.net-mvc-4,Asp.net Mvc 4,Hi all I am binding the img tag as follows <img alt="" src='@Url.Action("UserImage", "Home")' class="avatar img-circle img-thumbnail" /> This is my code to get the image from the database [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;

Hi all I am binding the

img
tag as follows

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

This is my code to get the image from the database

[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

enter image description here


#1

This code looks fine to me. Are you sure that your method is not returning NULL ? Did you try to access the Image source url in a browser ?

#2

So you want to know how to handle this when you know there is no image?

#3

Why don't you create a default avatar image to use when you don't have a user image? It's not a good idea to set image src to nothing. nczonline.net/blog/2009/11/30/…

#4

Hi shyju yes I am getting the controller with action where there is no image or when I am returning null

#5

Hi Emma Middlebrook I can do that but I would like to whether there is any possibility if we come across the scenario