2014-11-08 54 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