fill kendogrid with api controller in asp.net

偶尔善良 提交于 2019-12-12 02:07:34

问题


i use from kendo grid but my grid dont fill with values and when show page grid dont load. i use kendo 2014 and asp.net 2012. my api controller code :

public class ValuesControllerApi : ApiController
{
    public List<File> Get()
    {
        GuaranteeEntities ef = new GuaranteeEntities();
        var file = ef.Files.Where(c => c.UpdaterUserInfo == "Guarantee").ToList();
        return file;
    }
}

and my html Code is :

 <div id="employeesGrid">
                        <script>
                            $(function () {
                                $("#employeesGrid").kendoGrid({
                                    dataSource: new kendo.data.DataSource({
                                        transport: {
                                            read: "/api/ValuesControllerApi"
                                        }
                                    })
                                });
                            });

                            $(function () {
                                $("#employeesGrid").kendoGrid({
                                    columns: [
                                        { field: "Name" , title:"test" },
                                        { field: "Family", title: "test test" }
                            ],
                            dataSource: new kendo.data.DataSource({
                                transport: {
                                    read: "/api/ValuesControllerApi"
                                }
                            }),
                            sortable: true
                            });
                            });

                         </script>
                    </div>

回答1:


Try testing your API in the browser alone.

localhost:12345/api/ValuesControllerApi (change your debugging url as required)

Does that work? Chances are, it doesn't. The reason is because WebApi uses a default pattern for finding the controller end point. You can find more info here.

But to spare you the time, take note of this line:

To find the controller, Web API adds "Controller" to the value of the {controller} variable.

What this means is by default, WebApi assumes all controller classes end in "Controller" when trying to route to an endpoint. In your case, you have named your API ValuesControllerApi which ends in "Api". Remove the "Api" from the class name and it should work.

So, your class name should look like this: ValuesController : ApiController

and you call it like this: api/Values




回答2:


I think that your controller action won't give Kendo Grid the results in the format it expects. You need to send it a DataSourceRequest. There is a simple example here you may check.

Also this documentation article should shed some light on the issue too.

Hope it helps!



来源:https://stackoverflow.com/questions/23799214/fill-kendogrid-with-api-controller-in-asp-net

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