【问题标题】:KendoUI Grid fails to make Ajax call on the Page LoadKendoUI Grid 无法在页面加载时进行 Ajax 调用
【发布时间】:2015-03-04 20:48:07
【问题描述】:

我正在为 KendoUI 网格制作一个演示页面,我在下面的链接中遵循示例,几乎就像它是“逐字”一样。

http://demos.telerik.com/aspnet-mvc/grid/index

我不确定我做错了什么,但似乎 Grid 没有进行它应该进行的第一个 Ajax 调用。我对 MVC 和 Kendo 都很陌生,但不确定问题是否出在任何特定部分。让我从一些代码 sn-p 开始。我的 Index.CsHTMl 如下所示:

<!DOCTYPE html>
@using (Html.BeginForm())
{
    <div id="clientsDb">
        @(Html.Kendo().Grid<Prometheus.Core.Domain.Employee>()
              .Name("employeeGrid")
              .Columns(columns =>
              {
                  columns.Bound(c => c.Id).Width(140);
                  columns.Bound(c => c.FirstName).Width(190);
                  columns.Bound(c => c.LastName);
              })
              .HtmlAttributes(new {style = "height: 380px;"})
              .Scrollable()
              .Groupable()
              .Sortable()
              .Pageable(pageable => pageable
                  .Refresh(true)
                  .PageSizes(true)
                  .ButtonCount(5))
              .DataSource(dataSource => dataSource
                  .Ajax()
                  .Read(read => read.Action("ReadEmployee", "EmployeeGrid"))
              )
              )
    </div>
}

<style scoped>
    #clientsDb {
        width: 952px;
        height: 396px;
        margin: 20px auto 0;
        padding: 51px 4px 0 4px;
        background: url('@Url.Content("~/content/web/grid/clientsDb.png")') no-repeat 0 0;
    }
</style>

我的控制器如下所示:

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

        [HttpGet]
        public ActionResult ReadEmployee([DataSourceRequest]DataSourceRequest request)
        {
            return Json(GetEmployees().ToDataSourceResult(request),JsonRequestBehavior.AllowGet);
        }


        public static IEnumerable<Core.Domain.Employee> GetEmployees()
        {
            return new List<Core.Domain.Employee>
            {
                new Core.Domain.Employee
                {
                    Id = 1,
                    FirstName = "Someone",
                    LastName = "Else"
                },
                 new Core.Domain.Employee
                {
                    Id = 2,
                    FirstName = "FirstName",
                    LastName = "LastName"
                }
            };
        }

这几乎复制了演示中的行为:

  1. 加载 Index.cshtml 时没有从网格发出 Ajax 请求
  2. 当我自己调用 ReadEmployee ActionMethod 时,它会正确返回 Json。此外,当我单击“空”网格上的任何按钮时,它会加载相同的 JSON。
  3. 与示例不同,我添加了 BeginForm()。它不适用于有/没有 BeginForm。

有什么想法吗?

【问题讨论】:

    标签: ajax asp.net-mvc kendo-ui telerik kendo-grid


    【解决方案1】:

    默认情况下,读取操作是 POST 而不是 GET。因此,您要么需要从读取操作中删除 HttpGet Verb,要么添加 .Read(read =&gt; read.Action("ReadEmployee", "EmployeeGrid").Type(HttpVerbs.Get))

    到读取方法。

    希望这会有所帮助。

    对于此类问题,我喜欢使用 Fiddler 来检查正在发送的请求,并确保在适当的情况下我期待 POST、GET、DELETE 等。

    【讨论】:

      猜你喜欢
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多