【问题标题】:Kendo ui Grid shows json instead of grid Asp .net razorKendo ui Grid 显示 json 而不是网格 Asp .net razor
【发布时间】:2023-04-07 16:04:01
【问题描述】:

我试图初始化我的 Kendo ui 网格。我可以使用 View 对象填充它, 但是当我尝试以 Json 格式执行此操作时(即移动到下一页时),我得到一个显示 json 结果而不是我的视图的屏幕。

这是控制器代码:

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

而cshtml是:

@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"))
      ));

加载页面时调用的Index 操作效果很好,但是当我尝试移动到下一页时,调用了Camapigns_Read 操作,但我得到一个带有 json 结果的空白页面。我在这里错过了什么?

编辑:我想在服务器端执行分页

【问题讨论】:

  • 您是在做分页客户端还是服务器端?如果是客户端,您需要将以下内容添加到您的数据源中:.ServerOperation(false)

标签: razor asp.net-mvc-4 views kendo-ui


【解决方案1】:

知道了,

问题是必须首先初始化视图,并使用相同的控制器名称 - 该死的命名约定 :), 我的解决方案是

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

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

【讨论】:

  • 你好 briler。我面临着同样的问题。尝试了您的答案,但我只得到一个空网格。你能帮忙吗?
  • @AakashThakur - 当然,我可以试试,请分享你的代码,我会看看
  • 伙计,你刚刚救了我!我已经为此苦苦挣扎了好几个小时。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多