无法在Web网格中使用导航属性 [英] can not use navagation properties in web grid

查看:50
本文介绍了无法在Web网格中使用导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网络网格中,我无法在我的类(产品和productimages类)之间使用导航属性.例如,我在网络网格中使用了以下代码:

in web-grid i can not use navigation properties between my classes(products and productimages classes). for example i have used below code in web grid:

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

但是我得到了错误:

  'System.Collections.Generic.HashSet<WebStore.Models.ProductImage>' does not contain a definition for 'First'

我的总代码如下:

    @model IEnumerable<WebStore.Models.Product>

@使用System.Linq;

@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<WebStore.Models.Product>
  @using System.Linq
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutCategory.cshtml";
}

<br/>

<p>
@Html.ActionLink("Create New", "Create")
</p>

<div id="divGrid">

@{ Html.RenderPartial("_ProductTitle", Model); }
</div>

这是我的RenderPartial,它在发布之前使用Web网格:

this is my RenderPartial that use web grid before i posted:

    @model IEnumerable<WebStore.Models.Product>
    @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}))
 }
                    )

推荐答案

我从网上通过以下代码得到了我的问题的答案:

i got the answer of my question with below code from the net:

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

I followed your steps and test your code and the same error display. So I think the format in your code can not be used in webgrid. So I think about another way to meet your requirement: we need a ViewModel to display what you want to show in the View and search the first item in Controller then save as ViewModel and pass the ViewModel to View, then we do not need search in the View. Here I will show you the steps with my demo.

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

ViewModel.cs:

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. 我们可以初始化一些数据,并根据Controller中的Product搜索数量"的第一个值,并将其分配给ViewModel.

InventoryController .cs:

InventoryController .cs:

    public class InventoryController : Controller
    public ActionResult WebgridSample()
    {
        ObservableCollection<Product> inventoryList =
            new ObservableCollection<Product>();
        inventoryList.Add(new Product
        {
            Id = "P101",
            Name = "Computer",
            Description = "All type of computers",
            quantity = new List<Quantity>
            {
                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<Quantity>
            {
                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<Quantity>
            {
                new Quantity {QUAN = 700 },
                new Quantity {QUAN = 800 },
                new Quantity {QUAN = 900 }
            }
        });
        IEnumerable<string> model = (from sig in inventoryList
                                    select new ViewModel
                     {
                         Name = sig.Name,
                         Description = sig.Description,
                         quan = sig.quantity.FirstOrDefault(),
                     }).ToList();
        return View(model);
    }

  1. 我们可以在Webgrid中调用ViewModel的每个参数,而无需使用First().

WebgridSample.cshtml:

WebgridSample.cshtml:

   <div id="gridContent">
    @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 "</i>)
 ))

这篇关于无法在Web网格中使用导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆