0

net mvc4 项目与 Kendo UI iam 使用简单的 ajax 网格从数据库中打印值,但它没有显示在网格上我的代码是

<%: Html.Kendo().Grid<CustomerTest.Models.ProductViewModel>()
   .Name("grid")
      .DataSource(dataSource => dataSource
          .Ajax()
          .Read(read => read
                   .Action("Printc", "Home") // Set the action method which will return the data in JSON format
              // .Data("productsReadData") // Specify the JavaScript function which will return the data
          )
      )
      .Columns(columns =>
      {
          columns.Bound(product => product.CustomerID);
          columns.Bound(product => product.CustomerFName);
          columns.Bound(product => product.CustomerLName);
      })
      .Pageable()
      .Sortable()
%>

我的行动方法是

 public ActionResult Printc()
    {
       // ViewBag.Message = "Welcome to ASP.NET MVC!";

        return View(GetCustomers());
    }

    private static IEnumerable<ProductViewModel> GetCustomers()
    {
        var northwind = new CustomerLinqDataContext();
        var purchCount = northwind.Customer_details.Count();
        return northwind.Customer_details.Select(product => new ProductViewModel
        {
           CustomerID   = product.ID,
             CustomerFName = product.name,
             CustomerLName = product.lname,
             CustomerAge = product.age 

        });
    }

请有人帮我把我做错了什么?我试图在页眉上传递我的模型

 <%@ Page Title="" Language="C#" MasterPageFile="~/Areas/aspx/Views/Shared/Web.Master" 
    Inherits="System.Web.Mvc.ViewPage<IEnumerable<CustomerTest.Models.ProductViewModel>>" %> 

它工作得很好,但我的单页上有多个网格,它们来自不同的表,所以这就是为什么我想以不同的方式传递每个模型请有人在这段代码中帮助我谢谢

4

2 回答 2

1

问题出在您的 Printc 方法上。您必须返回为 Kendo Grid 创建的 Json 对象:

public ActionResult Index_Printc([DataSourceRequest] DataSourceRequest request)
{
    return Json(GetCustomers().ToDataSourceResult(request));
}

在我这边,我在 Ajax 请求上指定了 ID,我不知道它是否对读取请求是强制性的,但是如果方法的更新不起作用,请添加:

<%: Html.Kendo().Grid<CustomerTest.Models.ProductViewModel>()
   .Name("grid")
      .DataSource(dataSource => dataSource
          .Ajax()
          .Model(model => model.Id(p => p.CustomerID))
          .Read(read => read.Action("Printc", "Home") 
          )
      )
      .Columns(columns =>
      {
          columns.Bound(product => product.CustomerID);
          columns.Bound(product => product.CustomerFName);
          columns.Bound(product => product.CustomerLName);
      })
      .Pageable()
      .Sortable()
%>

希望能帮助到你 !

于 2013-11-08T09:27:48.683 回答
0

如果您在同一页面上有多个网格,请确保它们具有唯一的名称,并且并非所有网格都称为网格。

于 2013-11-08T09:25:38.083 回答