0
votes

i added a kendo grid in my view.The datasource of that grid is coming from the action method Products_Read in my controller.I had used entity data model in my project.I had added out controller and view code of my index action.The problem is that the data is not coming in grid.

Controller Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request)
        {
            using (var northwind = new productTableEntities())
            {
                IQueryable<product> products = northwind.products;
                DataSourceResult result = products.ToDataSourceResult(request);
                return Json(result);
            }
        }
    }
}

View Code(section where I have used Grid):

<%: Html.Kendo().Grid<MvcApplication1.Models.product>()
      .Name("grid")
      .DataSource(dataSource => dataSource // Configure the grid data source
          .Ajax() // Specify that ajax binding is used
          .Read(read => read.Action("Products_Read", "Home")) // Set the action method which will return the data in JSON format
       )
      .Columns(columns =>
      {
          columns.Bound(product => product.id);   
          columns.Bound(product => product.name);
      }).Pageable().Sortable() 
%>
2
1) Did you check browser console? Are there errors? 2) Did you check network traffic? Is Product_Read actually returning desired data? - Alberto
yes data is being returned from Product_Read - Ajay
OK. answer my point 1 too please. Another Q: is it the only element with that name ("grid")? - Alberto
yes there is only one element with the name "grid" - Ajay
Ok, good because Name sets id too and this ruins kendo scripts... Still: 1) Did you check browser console? Are there errors? - Alberto

2 Answers

2
votes

After a "comment-chat" with Ajay he identified the problem in 3rd party JS.

However Ajay I propose you a small enhancement in your Products_Read action: 1 line and with AllowGet behaviour.

using (var northwind = new productTableEntities())
        {
            return Json(northwind.products.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

Have a nice day,

Alberto

0
votes

You have used ActionResult return type for Products_Read Method. I would suggest you to change it to JsonResult.

public JsonResult Products_Read([DataSourceRequest]DataSourceRequest request)
{
    using (var northwind = new productTableEntities())
    {
        IQueryable<product> products = northwind.products;
        DataSourceResult result = products.ToDataSourceResult(request);
        return Json(result);
    }
}