简体   繁体   English

Kendo UI网格绑定

[英]Kendo UI Grid bind

I tried to bind Kendo Grid.but it shows me below error 我试图绑定Kendo Grid。但是它向我显示以下错误

The model item passed into the dictionary is of type 'System.Data.Entity.DbSet 1[KendoApp.Product]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[KendoApp.Models.ProductModels]'. 传递到字典中的模型项的类型为'System.Data.Entity.DbSet 1[KendoApp.Product]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [KendoApp.Models.ProductModels 1[KendoApp.Product]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable ]”。

My View 我的观点

@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()
)

My Controller 我的控制器

 public ActionResult KendoGrid()
    {

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

        var products = new northwindEntities().Products;
        ViewBag.Products = products;
        return View(products);

Product Model 产品型号

   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; }
}
    }

What is the error ? 有什么错误? how to fix that ? 如何解决?

Please try this, 请尝试一下

Your view: 您的看法:

@(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"))
    )
)

Your controller actions: 您的控制器动作:

public JsonResult ProductsRead([DataSourceRequest] DataSourceRequest request) 
{
    var products = new northwindEntities().Products
        .Select(p => new ProductModels {  
            ProductID = p.ProductID,
            ProductName = p.ProductName,
            UnitPrice = p.UnitPrice,
            UnitsInStock = p.UnitInStock
        })
        .AsQueryable();

    return Json(products.ToDataSourceResult(request));
}

public ActionResult KendoGrid()
{
    return View();
}

The type you are accepting into your view, and binding with the Grid, is a collection of KendoApp.Models.ProductModels. KendoApp.Models.ProductModels的集合是您接受到视图中并与Grid绑定的类型。 What you are passing from your controller is a collection of entities of type KendoApp.Product. 您从控制器传递的是KendoApp.Product类型的实体的集合。 They are not considered the same type (even if all the properties in these classes are) which is why you get the error. 它们不被视为同一类型(即使这些类中的所有属性都是相同的),这就是为什么您会收到错误消息。

What you need to do is transform the KendoApp.Product entities into a new IEnumerable collection of your ProductModels class in your controller. 您需要做的是将KendoApp.Product实体转换为控制器中ProductModels类的新IEnumerable集合。 Something like: 就像是:

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

The var products is now of type IEnumerable < KendoApp.Models.ProductModels > which will be accepted by your view. 现在,var产品的类型为IEnumerable <KendoApp.Models.ProductModels>,您的视图将接受该产品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM