Kendo MVC Ajax Grid:没有显示值

时间:2013-11-08 09:17:19

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 kendo-ui

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>>" %> 

它运行正常,但我的单页上有多个网格,而且它们来自不同的表格,这就是为什么我想以不同的方式传递每个模型,请有人在此代码中帮助我,谢谢

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

问题在于您的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()
%>

希望它有所帮助!