3

在尝试将 KendoUi Grid 与来自控制器的 Json 数据绑定时,我遇到了某个问题。事情似乎很好,我的 Json 对象包含数据,但网格仍然没有显示任何东西:

我在 chrome JavaScript 控制台中收到此错误:

GET http://localhost:8084/Records?take=5&skip=0&page=1&pageSize=5 500 (Internal Server Error) 

View

<div id="grid">
</div>
<div>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#grid").kendoGrid({
                dataSource: {
                    type: "json",
                    serverPaging: true,
                    pageSize: 5,
                    groupable: true,
                    selectable: "row",
                    transport: { read: { url: "Records", dataType: "json"} }
                },
                height: 400,
                scrollable: true,
                sortable: true,
                filterable: true,
                pageable: true,
                columns: [
                        { field: "No", title: "No" },
                        { field: "Desc", title: "Description" },
                        { field: "priority", title: "Priority" },
                        { field: "decision", title: "Decision" }
                    ],
                dataBound: function () {
                    this.expandRow(this.tbody.find("tr.k-master-row").first());
                }
            });
        });
    </script>

Controller

    public ActionResult GetRecords()
    {
       var obj = new User();
        var jsnRslt = obj.GetResult(Session["id"].ToString());
//return Json(jsnRslt);

        return Json(jsnRslt, JsonRequestBehavior.AllowGet); //Changed as suggested by Dismissile
    }

Model

public object GetResult(string usrId)
    {
…
….
…..            try
        {
            int i = 0;
            if (rcrds != null || rcrds.HasRows)
            {
                jsonWriter.WriteStartObject();
                while (rcrds.Read())
                {
                    for (int j = 0; j < rcrds.FieldCount; j++)
                    {
jsonWriter.WritePropertyName(rcrds.GetName(j));
jsonWriter.WriteValue(rcrds.GetValue(j));
                    }
                    i++;
                }
                jsonWriter.WriteEndObject();
            }

        }

        catch (Exception ex) { }
        return jsonWriter;
    }
}

请帮忙。

4

5 回答 5

1

尝试使用 dataSource 中的传输属性,如下所示:

 <script type="text/javascript">



    var dataSource = new kendo.data.DataSource({
        batch: true,
        schema: {
            model: {
                id: "EmployeeID",
                fields: {
                    EmployeeID: { editable: true, validation: { required: true } },
                    EmployeeName: { validation: { required: true } }

                }
            }
        },
        transport: {
            read: {
                url: "/Home/GetData",
                type: "GET"

            },
            update: {
                url: "/Home/Update",
                type: "POST",
                contentType: 'application/json'


            },
            destroy: {
                url: "/Home/Destroy",
                type: "POST",
                contentType: 'application/json'

            },


            create: {
                url: "/Home/Create",
                type: "POST",
                contentType: 'application/json'

            },


            pageSize: 1,





            parameterMap: function (options, operation) {
                if (operation !== "read" && options.models) {
                    return kendo.stringify(options.models) ;
                }
            }

        }




    });





    $(document).ready(function () {


        $("#grid").kendoGrid({
            dataSource: dataSource,
            navigatable: true,
            pageable: true,
            height: 430,
            sortable: true,
            toolbar: ["create", "save", "cancel"],
            columns: [

                { field: "EmployeeID", title: "Employee ID", width: 110 },
                { field: "EmployeeName", title: "Employee Name", width: 110 },

                { command: "destroy", title: "Delete", width: 90 }],
            editable: true,
            selectable: "multiple row",
            groupable: true,
            navigatable: true,
            filterable: true
        });
    });



</script>

控制器:

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        var employee = new Employee().GetEmployeeList();

        return View(employee);
    }

    [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult GetData()
    {
        var obj = new Employee();


        return Json(obj.GetEmployeeList(), JsonRequestBehavior.AllowGet);
    }

    [HttpPost]
    public JsonResult Update(List<Employee> model)
    {
        var obj = new Employee();

        //return View();
        return Json(obj);
    }

    [HttpPost]
    public JsonResult Create(List<Employee> model)
    {
        var obj = new Employee();

        return Json(obj);
    }

    public ActionResult Destroy(Employee model)
    {
        return View();
    }

}

从 index 方法返回一个 html 视图来保存网格 &

于 2013-08-14T09:00:30.213 回答
1

You probably need this in your JSON call:

return Json(jsnRslt, JsonRequestBehavor.AllowGet);

It looks like you are doing a GET call, and by default GET is not allowed on a JSON call.

于 2013-03-25T14:52:08.233 回答
0

我认为您还需要在 ActionMethod Parm 中添加数据源请求

 public ActionResult GetResult([DatasourceRequest]request, string usrId)
 return Json(jsnRslt.ToDatasourceResult(request), JsonRequestBehavior.AllowGet);

每个 kendogrid 都需要这个

于 2013-03-27T15:41:43.793 回答
0

您正在返回一个ActionResult,它应该是 a JsonResult

于 2015-02-26T15:15:33.413 回答
0

尝试这个

 transport: { read: { url: "Records", dataType: "jsonp"} }

尝试使用jsonp而不是json.

于 2013-08-14T09:53:06.453 回答