KendoUI Grid无法在页面加载上进行Ajax调用

时间:2015-03-04 20:48:08

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

我正在为KendoUI网格制作一个演示页面,我正在关注以下链接中的示例,因为它是“Verbatim”。

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

我不确定我做错了什么,但似乎Grid没有进行它应该做的第一个Ajax调用。我对MVC和剑道都很新,但不确定问题是否与任何特定部分有关。让我先从一些代码片段开始。我的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
  2. 时,网格没有发出Ajax请求
  3. 当我自己调用ReadEmployee ActionMethod时,它会正确返回Json。此外,当我单击“Empty”网格上的任何按钮时,它会加载相同的JSON。
  4. 与示例中不同,我添加了BeginForm()。它没有使用/不使用BeginForm。
  5. 有什么想法吗?

1 个答案:

答案 0 :(得分:2)

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

读取方法。

希望这会有所帮助。

对于这样的问题,我喜欢使用Fiddler检查发送的请求,并确保我期望在适当的情况下POST,GET,DELETE等。