我是 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