Search code examples
asp.net-mvc-3imagerazorsrcuser-profile

Toggle between Images asp.net MVC3


I have a place holder for a profile photo.I want it to get profile image from database (this works OK)<img src="@Url.Action("Thumbnail", "Photo", new { id = Model.Mebrief.myGuid, size = "small" })" alt="@Model.UserName" width="150" height="150"/>. Problem: If there is no profile image ,it should get default place holder image located here:

<img src="@Url.Content("~/Content/profile-template/img/friend_avatar_default.jpg")" alt="crazy" width="150" height="150"/>

All these are supposed to show in a div below:

<div id="profile">   </div>

NOTE: I am using .cshtml razer page.


Solution

  • You could try something like this in your controller.

     public ActionResult Photo(int photoId)
        {
            MyPhoto photo = null;
            try
            {
                MyPhoto = db.GetMyPhotoById(photoId);
                return new FileStreamResult(new MemoryStream(photo.Image), photo.ImageMimeType);
            }
            catch (Exception ex)
            {
                //use the default image
                return new FilePathResult(Url.Content("~/Content/profile-template/img/friend_avatar_default.jpg"), "image/jpg");
            }
        }
    

    It would be helpful to see what you are doing in your controller, but I'm guessing you are returning a FileStreamResult from the bytes you get back from the database.

    Hope this helps.