Kendo ui Grid shows json instead of grid Asp .net

2019-07-14 05:26发布

问题:

I trying to initialize my Kendo ui grid. I am able to populate it using the View object, but when I try doing it in Json format (i.e. when moving to next page) I get a screen showing json results instead of my view.

Here's the controller code:

 public class CampaignsController : Controller
    {
        //
        // GET: /Campaigns/


        [HttpGet]
        public ActionResult Index()
        {
            return View(GetAllCampaigns());
        }


        public ActionResult Campaigns_Read([DataSourceRequest] DataSourceRequest request)
        {
           DataSourceResult result = GetAllCampaigns().ToDataSourceResult(request);
           return Json(result, JsonRequestBehavior.AllowGet);        
        }


        private static IEnumerable<NH_Campaign> GetAllCampaigns()
        {
            List<NH_Campaign> result = null;
            if (MBPDataAccess.Instance.GetAll(out result))
            {
                return result;
            }
            return new List<NH_Campaign>();
        }

and the cshtml is :

@model IEnumerable<MBP.NH_Campaign>

<h2>View1</h2>


@(Html.Kendo().Grid(Model)
      .Name("CGrid")
      .Columns(columns =>
          {
              columns.Bound(p => p.CampaignID).Title("Id");
              columns.Bound(p => p.CampaignName).Title("Name");
              columns.Bound(p => p.ClickUrlC2C_OFF).Title("Click URL");
              columns.Bound(p => p.PlatformID).Title("Platform ID");
          })
      //.Groupable()
      .Pageable()
      //.Sortable()
      //.Filterable()
      .DataSource(dataSource => dataSource.Ajax().PageSize(2).Read(read => read.Action("Campaigns_Read", "Campaigns"))
      ));

the Index action that is called when the page is loaded works great, but when I try to move to the next page the Camapigns_Read action is called but I get a blank page with json results. What am I missing here?

Edit : i want to perform paging on server-side

回答1:

Got it ,

the issue was that the view must be initialized first, with the same controller name - damn this naming convention :), my solution was

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]    
        public ActionResult Index([DataSourceRequest] DataSourceRequest request)
        {
           DataSourceResult result = GetAllCampaigns().ToDataSourceResult(request);
           return Json(result, JsonRequestBehavior.AllowGet);        
        }