2014-04-07 108 views
1

我是MVC的新手。我的照片儲存在名爲「Resim」的表格中。 enter image description hereMVC中的顯示圖像[VARBINARY(MAX)]

我想從視圖中調用關聯記錄時看到圖片,我將使用其ID來調用它。這裏是我的控制器代碼:

public ActionResult Details(int id = 0) 
    { 
     SozlukEntities db = new SozlukEntities(); 
     KelimeTuru kelime = db.KelimeTuru.Find(id); 

     if (kelime == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(kelime); 
    } 

這是我認爲的代碼:

@model SozlukRG.Models.KelimeTuru 

@{ 
    ViewBag.Title = "Details"; 
} 

<h2>Details</h2> 


<fieldset> 
    <p>Move the mouse pointer over this paragraph.</p> 
    <legend>KelimeTuru</legend> 
    <div class="div1" style="display: table; background-color: #b0c4de;"> 
     <div class="div1" style="display: table-row;"> 
      <div class="div1" style="display: table-cell; padding: 10px;"> 

       @Html.DisplayNameFor(model => model.KelimeId) 
      </div> 





      <div class="div1" style="display: table-cell; padding: 10px;"> 
       @Html.DisplayNameFor(model => model.VideoId) 
      </div> 


      <div class="div1" style="display: table-cell; padding: 10px;"> 
       @Html.DisplayNameFor(model => model.ResimId) 
      </div> 




      <div class="div1" style="display: table-cell; padding: 10px;"> 
       @Html.DisplayNameFor(model => model.Anlam) 
      </div> 
     </div> 

      <div class="div1" style="display: table-row;"> 
       <div class="div1" style="display: table-cell; padding: 10px;"> 
        @Html.DisplayFor(model => model.KelimeId) 

       <div class="div1" style="display: table-cell; padding: 10px;"> 
        <video width="320" height="240" controls> 
         <source src="/Videos/b.mp4" type="video/mp4"> 

        </video> 
       </div> 
       <div class="div1" style="display: table-cell; padding: 10px;"> 
        @*The Image will be inserted here, PLEASE HELP ME WITH THIS :) *@ 
       </div> 


       <div class="div1" style="display: table-cell; padding: 10px;"> 
        @Html.DisplayFor(model => model.Anlam) 
       </div> 

     </div> 
     </div> 

</fieldset> 
<p> 

    @Html.ActionLink("Back to List", "Index") 

</p> 

Resim是表的名稱和AdiVARBINARY(MAX)柱與圖片。我想在視圖中看到圖片。 事先請Help..Thanks ...

回答

2

您可以創建它處理顯示圖像的專用圖像控制器:

public class ImageController : Controller 
{ 
    public ActionResult Show(int id) 
    { 
     SozlukEntities db = new SozlukEntities(); 
     KelimeTuru kelime = db.KelimeTuru.Find(id); 
     byte[] data = kelime.Aidi; 

     return File(data, "image/jpg"); 
    } 
} 

我跳過錯誤檢查的清晰度。此外,退貨類型可能與"image/jpg"不同。

+0

這是正確的嗎? ' 公共的ActionResult showImg(INT ID) { SozlukEntities分貝=新SozlukEntities(); KelimeTuru kelime = db.KelimeTuru.Find(id); var stream = kelime.Resim.Adi; return File(stream,「image/jpeg」); } @ –

+1

@RuhiGoktas是的,應該這樣做。 –