【问题标题】:Bind kendo-ui grid from apicontroller从 apicontroller 绑定 kendo-ui 网格
【发布时间】:2014-06-13 13:56:45
【问题描述】:

我正在使用 Kendo-ui JQuery 版本,并且正在尝试从 ApiController 填充 kendo-ui 网格。我的网格仍然是空的...我错过了什么?

这是我的 ApiController 的结果:~/api/Countries :

[{"Id":4,"Name":"Germany"},
 {"Id":5,"Name":"China"},
 {"Id":6,"Name":"Myanmar"}]

这是我的 ApiController 代码:

public class CountriesController : ApiController
{
    private DBContext db = new DBContext();

    // GET api/Countries
    [Queryable]
    public IQueryable<Country> GetCountries()
    {
        return db.Countries;
    }
}

这是我的cshtml代码:

<script type='text/javascript'>

    $(document).ready(function () {
        $("#grid").kendoGrid({
            columns: [
                { field: "Id", title: "id" },
                { field: "Name", title: "name" }
            ],
            dataSource: new kendo.data.DataSource({
                transport: {
                    read: "api/Countries"
                },
                schema: {
                    model: {
                        id: "Id",
                        fields: {
                            Id: { type: "number" },
                            Name: { type: "string" }
                        }
                    }
                },
                pageSize: 3
            }),
            pageable: true
        });
    });

</script>

感谢您的帮助。

【问题讨论】:

  • 看网络流量,是否有对api/Countries的请求,如果有,响应码是什么?
  • 谢谢Robin,我平时会检查网络流量,但这次没有做。不知道为什么!!无论如何,响应是空的,因为在我的情况下 url 应该是“/api/Countries”而不是“api/Countries”。有效!

标签: asp.net-mvc-4 asp.net-web-api kendo-ui kendo-grid asp.net-apicontroller


【解决方案1】:

在我的来自剑道数据源的 API 调用中,我总是必须指定它是一个 json 返回数据类型,我认为它默认为 jsonp。

dataSource: new kendo.data.DataSource({
  transport: {
    read: {
         url: "/api/Countries",
         dataType: 'json'
      }
  },
  schema: {
    model: {
      id: "Id",
      fields: {
        Id: { type: "number" },
        Name: { type: "string" }
      }
    }
  },
  pageSize: 3
}),

【讨论】:

  • 如前所述(其他评论),问题在于 url “api/Countries” 而不是“/api/Countries”。
【解决方案2】:

Kendo Grid 没有以正确的格式恢复 Json。 请务必使用 KendoMVC DataSourceRequest 对象以正确的格式返回数据以供 Grid 使用。

这是一个例子:

public ActionResult Update([DataSourceRequest] DataSourceRequest request, MyViewModel data)
    {
        var result = UpdateBackend(data);
        return Json(result.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }

查看 MVC 的 Kendo 演示页面以获取更多示例: http://demos.telerik.com/aspnet-mvc/grid/index

【讨论】:

  • 谢谢乔纳森。我有点失落。我正在使用 JQuery Kendo UI,所以客户端站点。我以为我不需要服务器端 KendoMVC。我错了吗 ?此外,我的问题专门针对 ApiController。通过在 Kendo 支持的 json 结果上添加类似 {"data": 的内容,我已经成功地使用了标准 MVC 控制器。如果我是正确的,ApiController 调用的结果可以是 json 以外的其他格式。除了使用 DataSourceRequest 发送 Kendo 支持的结果之外,没有其他方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多