如何使用linq在webgrid中显示图像

时间:2017-02-19 10:51:33

标签: asp.net-mvc linq

我有2个表,第一个是产品,第二个是productImages 产品和productImages表之间存在一对多的关系。我的意思是每个productID我有几个图像。现在在我的索引视图的Web网格中,我想在webgrid中显示第一个图像。但我不知道如何在webgrid中使用linq for image专栏。 我试过这个但是没有用。

 grid.Column("", "Image", @<text><img src="@(Url.Content(Constants.ProductImagePath) + item.ProductImages.First().FileName)" width="100" /></text>)
下面的

是我的webgrid:

 @model IEnumerable<WebStore.Models.Product>



@{
    var grid = new WebGrid(source: Model, rowsPerPage: 5,ajaxUpdateContainerId:"divGrid");

}

@grid.GetHtml(tableStyle: "gridStyle", headerStyle: "gridHeader", rowStyle:   "gridRow", alternatingRowStyle: null,htmlAttributes:new{Id="divGrid"},
  columns: new WebGridColumn[] {
grid.Column("ProductName", "Product Name"),
grid.Column("Price", "Price"),
grid.Column("Description", "Description"),
grid.Column("CategoryName","Category Name",x=>x.Category.CategoryName),
grid.Column("", "Image", @<text><img   src="@(Url.Content(Constants.ProductImagePath) +     item.ProductImages.First().FileName)" width="100" /></text>),



grid.Column("","",x=>Html.ActionLink("Edit", "Edit", new{id=x.Id})),
grid.Column("","",x=>Html.ActionLink("Details", "Details", new{id=x.Id})),
grid.Column("","",x=>Html.ActionLink("Delete", "Delete", new{id=x.Id}))

}                         )

产品类:

  public Product()
    {
        this.OrderItems = new HashSet<OrderItem>();
        this.ProductImages = new HashSet<ProductImage>();
        this.BasketItems = new HashSet<BasketItem>();
    }

    public int Id { get; set; }
    public string ProductName { get; set; }
    public string Description { get; set; }
    public int Price { get; set; }
    public Nullable<int> CategoryID { get; set; }

    public virtual Category Category { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<OrderItem> OrderItems { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<ProductImage> ProductImages { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<BasketItem> BasketItems { get; set; }
}

}

产品形象类:

   public partial class ProductImage
{
    public int Id { get; set; }
    public int ProductID { get; set; }
    public string FileName { get; set; }
    public string Extension { get; set; }

    public virtual Product Product { get; set; }
}

}

0 个答案:

没有答案