How to pass array object as a parameter in kendo datasource read methods?

人盡茶涼 提交于 2020-03-05 06:08:18

问题


I am trying to send array object to mvc controller from kendo datasource read methods and unable to bind parameter studentId in GetAllStudentDetails Methods.

 var studentId=[1,2,3]
         var preSelectStudentDataSource = new kendo.data.DataSource({
                            transport: {
                                read: {
                                    url: '/Manage/Students/GetAllStudentDetails',
                                    dataType: "json",
                                    contentType: "application/json;charset=utf-8",
                                    data: { studentId: studentId}
                                }
                            },
                            schema: {
                                success: "success",
                                message: "message",
                                data: "data",
                                model: {
                                    id: "StudentId"
                                }
                            },
                            autoBind: true
                        })

In Controller Side

 public JsonResult GetAllStudentDetails(int[] studentId)
        {
            JsonResult result = null;
            // Code
            return result;
        }

回答1:


Making traditional=true it work for me

 var studentId=[1,2,3]
     var preSelectSegmentDataSource = new kendo.data.DataSource({
                        transport: {
                            read: {
                                url: '/Manage/Students/GetAllStudentDetails',
                                dataType: "json",
                                traditional: true,
                                contentType: "application/json;charset=utf-8",
                                data: { studentId: studentId}
                            }
                        },
                        schema: {
                            success: "success",
                            message: "message",
                            data: "data",
                            model: {
                                id: "StudentId"
                            }
                        },
                        autoBind: true
                    })

Or with JSON.stringify()

 var preSelectedSegmentDataSource = new kendo.data.DataSource({
                    transport: {
                        read: {
                            url: '/Manage/Students/GetAllStudentDetails',
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json;charset=utf-8",

                        },
                        parameterMap: function (options, operation) {
                           var studentId=[1,2,3]
                            if (operation == "read") {

                                return JSON.stringify({ studentId: studentId});
                            }
                        }
                    },
                    schema: {
                        success: "success",
                        message: "message",
                        data: "data",
                        model: {
                            id: "StudentId"
                        }
                    },
                    autoBind: true
                })


来源:https://stackoverflow.com/questions/56382671/how-to-pass-array-object-as-a-parameter-in-kendo-datasource-read-methods

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