2012-06-08 83 views
0

我有一個檔案照片的佔位符。我希望它從數據庫中獲取檔案圖像(這工作正常)<img src="@Url.Action("Thumbnail", "Photo", new { id = Model.Mebrief.myGuid, size = "small" })" alt="@Model.UserName" width="150" height="150"/>。 問題: 如果沒有輪廓的圖像,它應該得到默認的佔位符圖像位於:切換圖像asp.net MVC3

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

所有這些都應該在下面一個div顯示:

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

注:我使用.cshtml razer頁面。

回答

0

你可以嘗試在你的控制器是這樣的。

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"); 
     } 
    } 

看看你在控制器中做了什麼會很有幫助,但是我猜你正在從你從數據庫中得到的字節中返回一個FileStreamResult。

希望這會有所幫助。

+0

工程像魅力,..非常感謝 – user1350968

0
下面

不是有效的剃刀語法,但只是一個想法 - 用三元操作

if true ? new image : default image 

型號= NULL

!?
img src =「@ Url.Action(」Thumbnail「,」Photo「,new {id = Model.Mebrief.myGuid,size =」small「})」alt =「@ Model.UserName」width =「150 「HEIGHT =」 150 「/>」

IMG SRC = 「@ Url.Content(」 〜/內容/輪廓模板/ IMG/friend_avatar_default.jpg 「)」 ALT =「瘋狂「WIDTH =」 150" HEIGHT = 「150」/>