无法在网格中使用导航属性

在网格中我不能在我的类(产品和productimages类)之间使用导航属性。 例如我在网格中使用了以下代码:

grid.Column("", "test",item=> (item.ProductImages.First().Id)+(item.Price)) 

但是我得到了错误:

  'System.Collections.Generic.HashSet' does not contain a definition for 'First' 

我的总代码如下:

  @model IEnumerable 

@using System.Linq;

  @{ 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("", "test",item=> (item.ProductImages.First().Id)+(item.Price)), 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})) } ) 

这是我的索引视图:

  @model IEnumerable @using System.Linq @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_LayoutCategory.cshtml"; } 

@Html.ActionLink("Create New", "Create")

@{ Html.RenderPartial("_ProductTitle", Model); }

这是我发布之前使用网格的RenderPartial:

  @model IEnumerable @using System.Linq @ViewBag.test @{ 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("", "test",item=>(int) (item.ProductImages.FirstOrDefault().Id)+(int)(item.Price)), 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})) } ) 

这是因为First的定义在System.Linq可用。 所以,你应该在你的剃刀页面中有System.Linq:

 @using System.Linq; 

如果您在多个页面中使用Linq,则可以在web.config中添加System.Linq命名空间,这样您就不需要在每个页面中使用上面的内容。 您可以在以下配置中在web.config中添加命名空间:

        

我从下面的代码中得到了我的问题的答案:

我按照你的步骤测试你的代码和相同的错误显示。 所以我认为代码中的格式不能在webgrid中使用。 所以我想到了另一种满足你需求的方法:我们需要一个ViewModel来显示你想要在View中显示的内容并搜索Controller中的第一项然后保存为ViewModel并将ViewModel传递给View,那么我们就不需要搜索了风景。 在这里,我将向您展示我的演示步骤。

  1. 现在,我们有两个型号产品和数量,一个产品有很多数量。 我们应该创建一个ViewModel。

ViewModel.cs:

  public class ViewModel { public string Id { get; set; } public string Name { get; set; } public string Description { get; set; } public long quan { get; set; } } 
  1. 我们可以初始化一些数据并搜索Quantity的第一个值取决于Controller中的Product并分配给ViewModel。

InventoryController .cs:

  public class InventoryController : Controller public ActionResult WebgridSample() { ObservableCollection inventoryList = new ObservableCollection(); inventoryList.Add(new Product { Id = "P101", Name = "Computer", Description = "All type of computers", quantity = new List { new Quantity {QUAN = 100 }, new Quantity {QUAN = 200 }, new Quantity {QUAN = 300 } } }); inventoryList.Add(new Product { Id = "P102", Name = "Laptop", Description = "All models of Laptops", quantity = new List { new Quantity {QUAN = 400 }, new Quantity {QUAN = 500 }, new Quantity {QUAN = 600 } }, }); inventoryList.Add(new Product { Id = "P103", Name = "Camera", Description = "Hd cameras", quantity = new List { new Quantity {QUAN = 700 }, new Quantity {QUAN = 800 }, new Quantity {QUAN = 900 } } }); IEnumerable model = (from sig in inventoryList select new ViewModel { Name = sig.Name, Description = sig.Description, quan = sig.quantity.FirstOrDefault(), }).ToList(); return View(model); } 
  1. 我们可以在不使用First()的情况下在webgrid中调用ViewModel的每个参数。

WebgridSample.cshtml:

  
@grid.GetHtml(tableStyle: "webGrid", headerStyle: "header", alternatingRowStyle: "alt", selectedRowStyle: "select", columns: grid.Columns( grid.Column("Id", ), grid.Column("Name", " Name"), grid.Column("Description", "Description", style: "description"), grid.Column("Quantity", "quan ") ))