Kendo ui Grid shows json instead of grid Asp .net razor

*爱你&永不变心* 提交于 2020-01-04 15:16:27

问题


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);        
        }


来源:https://stackoverflow.com/questions/16143886/kendo-ui-grid-shows-json-instead-of-grid-asp-net-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!