user1073794
user1073794

Reputation: 87

Kendo MVC Ajax Grid : Values are not showing

net mvc4 project with Kendo UI iam using a simple ajax grid to print values from the database but it is not showing on the grid my code is

<%: 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()
%>

and my action method is

 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 

        });
    }

please some one help me put what i am doing wrong? i tried to pass my model on the page header

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

It worked fine but i have mutiple grids on my single page and they are coming from different tables so thats why i want to pass each model differently please someone help me in this code thanks

Upvotes: 0

Views: 1352

Answers (2)

Joffrey Kern
Joffrey Kern

Reputation: 6499

The problem is on your Printc method. You have to return a Json object create for the Kendo Grid :

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

On my side, I specify the ID on the Ajax request, I don't know if it's mandatory for a read request, but if the update of the method doesn't work, add this :

<%: 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()
%>

Hope it helps !

Upvotes: 1

user1540857
user1540857

Reputation: 194

if you have several grids on the same page make sure that they have unique names and not all of them are called grid.

Upvotes: 0

Related Questions