C# 剑道UI网格绑定

C# 剑道UI网格绑定,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我试图绑定剑道网格,但它显示了下面的错误 传递到字典中的模型项的类型为“System.Data.Entity.DbSet1[KendoApp.Product]”,但此字典需要类型为“System.Collections.Generic.IEnumerable1[KendoApp.Models.ProductModels]”的模型项 我的看法 @model IEnumerable<KendoApp.Models.ProductModels> @(Html.Kendo().Grid(M

我试图绑定剑道网格,但它显示了下面的错误

传递到字典中的模型项的类型为“System.Data.Entity.DbSet
1[KendoApp.Product]”,但此字典需要类型为“System.Collections.Generic.IEnumerable
1[KendoApp.Models.ProductModels]”的模型项

我的看法

@model IEnumerable<KendoApp.Models.ProductModels>

@(Html.Kendo().Grid(Model)
.Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.ProductID);
        columns.Bound(p => p.ProductName);
        columns.Bound(p => p.UnitPrice);
        columns.Bound(p => p.UnitsInStock);
    }).Pageable()
)
@model IEnumerable
@(Html.Kendo().Grid(模型)
.名称(“网格”)
.列(列=>
{
columns.Bound(p=>p.ProductID);
columns.Bound(p=>p.ProductName);
columns.Bound(p=>p.UnitPrice);
columns.Bound(p=>p.UnitsInStock);
}).Pageable()
)
我的控制器

 public ActionResult KendoGrid()
    {

        //IEnumerable<Product> products = new northwindEntities().Products;

        var products = new northwindEntities().Products;
        ViewBag.Products = products;
        return View(products);
public ActionResult KendoGrid()
{
//IEnumerable products=新的northwindEntities();
var products=新的northwindEntities().产品;
ViewBag.Products=产品;
返回视图(产品);
产品模型

   public class ProductModels
{
    public int ProductID { get; set; }
    public string ProductName { get; set; }
    public Nullable<int> SupplierID { get; set; }
    public Nullable<int> CategoryID { get; set; }
    public string QuantityPerUnit { get; set; }
    public Nullable<decimal> UnitPrice { get; set; }
    public Nullable<short> UnitsInStock { get; set; }
    public Nullable<short> UnitsOnOrder { get; set; }
    public Nullable<short> ReorderLevel { get; set; }
    public bool Discontinued { get; set; }
}
    }
公共类产品模型
{
public int ProductID{get;set;}
公共字符串ProductName{get;set;}
公共可为空的供应商ID{get;set;}
公共可空类别ID{get;set;}
公共字符串QuantityPerUnit{get;set;}
公共可为空的单价{get;set;}
公共可空单位库存{get;set;}
公共可为空的UnitsOnOrder{get;set;}
公共可为空的重新排序级别{get;set;}
公共布尔已中断{get;set;}
}
}
错误是什么?如何解决这个问题?

请试试这个

你的看法:

@(Html.Kendo().Grid<KendoApp.Models.ProductModels>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.ProductID);
        columns.Bound(p => p.ProductName);
        columns.Bound(p => p.UnitPrice);
        columns.Bound(p => p.UnitsInStock);
    })
    .Pageable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("ProductsRead", "YourControllerName"))
    )
)

您在视图中接受并与网格绑定的类型是KendoApp.Models.ProductModels的集合。从控制器传递的是KendoApp.Product类型的实体集合。它们不被认为是相同的类型(即使这些类中的所有属性都是相同的),这就是为什么会出现错误的原因

您需要做的是将KendoApp.Product实体转换为控制器中ProductModels类的新IEnumerable集合。比如:

var products = new northwindEntities().Products.Select
    (x => new KendoApp.Models.ProductModels
        {
            ProductID = x.ProductID,
            ProductName = x.ProductName,
            ...
            Discontinued = x.Discontinued
        }
    ).ToList();
var产品现在的类型是IEnumerable,您的视图将接受该类型

var products = new northwindEntities().Products.Select
    (x => new KendoApp.Models.ProductModels
        {
            ProductID = x.ProductID,
            ProductName = x.ProductName,
            ...
            Discontinued = x.Discontinued
        }
    ).ToList();