2014-11-08 219 views
0

我'新在ASP.net ...我試圖從一個MySQL數據庫顯示圖像,但沒有顯示... 在我的產品類別我有一個圖片的列表:從數據庫顯示的圖像在剃刀/ MVC5(Asp.net)

public partial class product 
{ 
    public product() 
    { 
     this.orderitems = new List<orderitem>(); 
     this.pictures = new List<picture>(); 
     this.productitemsuppliers = new List<productitemsupplier>(); 
     this.recommendations = new List<recommendation>(); 
     this.reviews = new List<review>(); 
     this.productitems = new List<productitem>(); 
    } 

我habve這個方法在我的ProductController的:

 static IDatabaseFactory dbFact = new DatabaseFactory(); 
     IUnitOfWork utOfW = new UnitOfWork(dbFact); 

     public FileContentResult imageGenerate(int id) 
     { 
      byte[] image = utOfW.ProductRepository.GetById(id).pictures.ElementAt(0).picture1; 
       // Another way to get the Byte Array from the Database... 
      // byte[] image = utOfW.PictureRepository.GetMany(p=>p.product.idProduct==id).ElementAt(0).picture1; 
    if (image != null) { 
      return new FileContentResult(image, "image/jpg"); 
     } 
      return null; 
     } 

在我看來:

@foreach (var item in Model) { 
.... 
    <td> 
<img id="image" src='@Url.Action("imageGenerate", "ProductController", new { id = item.idProduct})'/> 
    </td> 

} 

這似乎是確定,但reseult是不是...

enter image description here

感謝您的幫助

回答

0

首先,請檢查您是否可以使用只是簡單的web請求查看該文件,例如http://youraddress/Product/imageGenerate/5。如果你不能,那麼這是生成圖像的問題。

如果你能看到圖像,那麼很可能你正在使用ProductController' in your view. You only need to specify name of the controller without extension Controller`。所以,你的圖像的代碼應該如下所示:

<img id="image" src='@Url.Action("imageGenerate", "Product", new { id = item.idProduct})'/> 
0

@dotnetom 首先感謝您快速的答案... 我改變了呼籲:

<img id="image" src='@Url.Action("imageGenerate", "Product", new { id = item.idProduct})'/> 

但現在我有一個又一個問題:

enter image description here

但我敢肯定,此方法我曾經在我的索引測試它的ProductController的

public ActionResult Index() 
     { 
      var products = db.products.Include(p => p.category).Include(p => p.promotion).Include(p => p.user); 
      ViewBag.Count = utOfW.ProductRepository.GetById(1).pictures.Count(); // the result is 3 and it is true 

      return View(products.ToList()); 
     } 
的ThOD