gpt4 book ai didi

asp.net-mvc - 剑道,如何使用 mvc4 助手进行网格服务器分页

转载 作者:行者123 更新时间:2023-12-01 08:34:54 25 4
gpt4 key购买 nike

我正在使用 mvc4。在我的一个页面上,它有 Kendo Grid。我想每页显示 5 行。但是,如果我使用的是 mvc 助手,我使用纯 javascript 来做这件事没有问题。我迷路了,在网上找不到任何 sample 。

这是我的 javascript 代码。

        <script language="javascript" type="text/javascript">

$(document).ready(function () {
$("#grid").kendoGrid({
dataSource: {
type: "json",
serverPaging: true,
pageSize: 5,
transport: { read: { url: "Products/GetAll", dataType: "json"} },
schema: { data: "Products", total: "TotalCount" }
},
height: 400,
pageable: true,
columns: [
{ field: "ProductId", title: "ProductId" },
{ field: "ProductType", title: "ProductType" },
{ field: "Name", title: "Name" },
{ field: "Created", title: "Created" }
],
dataBound: function () {
this.expandRow(this.tbody.find("tr.k-master-row").first());
}
});
});

现在如果我使用 mvc 助手

            @(Html.Kendo().Grid(Model)
.Name("Grid") //please help me to finish the rest

更新:

添加操作代码。

    [HttpPost]
public ActionResult GetAll([DataSourceRequest]DataSourceRequest request, int id)
{
var products= ProductService.GetAll(id);

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

服务层的GetAll方法:

    public IQueryable<Product> GetAll(int id)
{
var products= ProductRepository.Get(p => p.Id== id && p.IsActive == true, null, "ProductionYear")
.OrderBy(o => o.Name); //.ToList();

return Product.Select(p => new ProductVM()
{
Name = p.Name,
ProductionYear= p.ProductionYear.Year.ToString()
Id = p.Id
}).AsQueryable();
}

现在,如果我运行该应用程序,我将收到以下错误:

“LINQ to Entities 无法识别方法 'System.String ToString()' 方法,并且该方法无法转换为存储表达式。”}

在GetAll 方法中,我注释掉了“ToList()”。如果我使用 ToList,一切正常。但我将查询所有行,而不是只查询该页面所需的那些行。

最佳答案

您可以在 DataSource method 中设置 PageSize .所以你需要这样的东西:

@(Html.Kendo().Grid(Model)
.Name("Grid")
.DataSource(dataSource => dataSource.Ajax()
.PageSize(5)
.Read(c => c.Action("GetAll", "Products")
.Model(s => s.Id(m => m.Id)))
.Columns(columns =>
{
columns.Bound(m => m.ProductId).Title("ProductId");
//other colums
})
.Events(g => g.DataBound("somefunction"))
.Pageable(true))

您可以在 KendoUI Grid 的 Asp.NET MVC wrappers documentation 中找到更多信息.

关于asp.net-mvc - 剑道,如何使用 mvc4 助手进行网格服务器分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12714872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com