0

我是 WebApi 的新手,收到的帖子参数有问题。我想用从 webapi 控制器方法接收到的数据填充 jqgrid。

网格代码如下:

buildPostData: function (postData) {

            var parameters = {
                page: postData.page,
                rows: postData.rows,
                sidx: '',
                sord: '',
                _search: false,
                searchField: '',
                searchString: '',
                searchOper: '',
                filter: { groupOp: '', rules: [] }
            };

            $.extend(parameters, postData);

            var data = {
                parameters: parameters
            };

            return JSON.stringify(data);
        }

bindGridAllData: function () {

            var viewModel = this;

            jQuery("#AllDataGrid").jqGrid({
                url: 'api/DataApi/GetAllData',
                datatype: 'json',
                mtype: 'POST',
                ajaxGridOptions: {
                    contentType: 'application/json; charset=utf-8'
                },
                serializeGridData: viewModel.buildPostData,
                serializeRowData: function (data) {
                    return JSON.stringify(data);
                },
                jsonReader: { repeatitems: false, root: "d.rows", page: "d.page", total: "d.total", records: "d.records" },
                colNames: ['DataId', 'Title', 'Description', 'Date', 'UserId', 'Name', 'Surname', 'PicturePath', 'NumberOfComments', ''],
                colModel: [
                        { name: 'DataId', index: 'DataId', width: 10, hidden: true, editable: false, sortable: false, key: true },
                        { name: 'Title', index: 'Title', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
                        { name: 'Description', index: 'Description', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
                        { name: 'Date', index: 'Date', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
                        { name: 'UserId', index: 'UserId', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
                        { name: 'Name', index: 'Name', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
                        { name: 'Surname', index: 'Surname', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
                        { name: 'PicturePath', index: 'PicturePath', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
                        { name: 'NumberOfComments', index: 'NumberOfComments', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
                        {
                            name: 'DataInformation', index: 'DataInformation', width: 200, editable: false, align: 'left', sortable: true, formatter: function (cellvalue, options, rowObject) {
                                var template = $('<div data-bind="template: { name: \'DataRecord\', data: DataRecord, ajax: { async: false } }"></div>');
                                var data = { DataRecord: rowObject };
                                ko.applyBindings(data, template[0]);

                                return template.html();
                            }
                        }
                ],
                height: '100%',
                width: 900,
                pager: '#resultGridAllDataPager',
                rowNum: 10,
                rownumbers: false,
                scroll: false,
                sortname: 'Date',
                sortorder: "asc",
                gridview: true,
                hoverrows: false,
                viewrecords: true,
                cmTemplate: { title: false },
                loadComplete: function () {
                }
            });
        }

而WebApi方法是:

    [HttpPost]
    public JQGridData GetAllData(JQGridSearchParameters parameters)
    {
        using (dataMKbazaEntities context = new dataMKbazaEntities())
        {
            JQGridData grid = new JQGridData();

            var allData = context.ispolniSITEpodatoci().ToList();

            List<object> data = new List<object>();

            foreach (var ad in allData)
            {
                data.Add(new {
                    DataId=ad.podatokID,
                    Title=ad.naslov,
                    Description=ad.opis,
                    Date=ad.datum,
                    UserId=ad.idKORISNIK,
                    Name=ad.ime,
                    Surname=ad.prezime,
                    PicturePath=ad.slika,
                    NumberOfComments=ad.brojKOMENTARI
                });
            }

            grid.rows = data;
            grid.page = parameters.page;
            grid.records = data.Count;
            grid.total = (int)Math.Ceiling((float)data.Count / (float)parameters.rows);

            return grid;
        }
    }

发送数据时,参数具有从buildPostData方法定义的值,但在 WepApi 控制器方法中接收时,它们为空。

我尝试过使用[FromBody]webapi 方法,但没有成功。http请求中的状态码是200 OK

4

1 回答 1

0

确保以下几点:

  1. 您的原始请求具有 Content-Type 标头。查看您的客户端代码,您似乎正在发送它,但只是想确保它确实是通过网络发送的。Web API 有一个错误,如果您不发送 Content-Type 标头,因为我们不知道要反序列化哪个格式化程序,我们默认为 C# 类型的默认值。在这种情况下,它可能是“空”。这已在稍后修复,但尚未公开。

  2. 进行以下检查以查看任何模型状态错误:

    if (!ModelState.IsValid) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState)); }

于 2013-05-30T21:15:40.620 回答