我通过ajax发送一个数组对象而C#MVC无法理解。 我尝试了几种方法,但没有用。我做错了什么?
我已经拥有和没有JSON.stringify,并且无法正常工作。变量teste01
收到null。
对象类:
public class Teste
{
public int dataid {get;set;}
public string datapackage { get; set;}
public int packageid {get;set;}
}
位指示:
[HandleError]
[AcceptVerbs(HttpVerbs.Get)]
[Authorize]
public JsonResult ListaGenerica(DataTables param, string aController, Teste[] teste01, bool porID = false, string regra = "", int filtroID = 0, string tipo = "")
JQuery函数:
function Controller()
{
return "EmpresaCarga";
}
function Dados()
{
var localproducts = [];
localproducts.push({
'dataid' : 1,
'datapackage' : 'test',
'packageid' : 3
});
localproducts.push({
'dataid' : 2,
'datapackage' : 'test 01',
'packageid' : 4
});
return localproducts;
}
JQuery的:
var oTable = $('#listagem').dataTable({
"bServerSide": true,
"sAjaxSource": '@Url.Action("ListaGenerica", "Ajax")',
"fnServerParams": function ( aoData ) {
aoData.push( { "name": "filtroID", "value": $("#ClienteID").find('option:selected').val() } );
aoData.push( { "name": "aController", "value": Controller() } );
aoData.push( { "name": "teste01", "value": Dados() } );
},
"bProcessing": true,
"sPaginationType": "full_numbers",
"aoColumns": [
{ "mDataProp": "Carga", "sTitle": "Carga" },
{ "mDataProp": "DtCarga", "sTitle": "DtCarga", "mRender": function (data, type, full) { return dtConvFromJSON(data); } },
{ "mDataProp": "Empresa", "sTitle": "Empresa" },
{ "mData": null, "bSortable": false, "mRender": function (data, type, row) { return '<a class="btn btn-default btn-xs" href="/Produto/Detalhar/' + row.EmpresaCargaID + '" title="Clique para abrir"><span class="glyphicon glyphicon-edit"></span></a>';}}
],
});
查询字符串参数
sEcho:2
.
.
.
filtroID:
aController:EmpresaCarga
aController:EmpresaCarga
teste01:[{"dataid":1,"datapackage":"test","packageid":3},{"dataid":2,"datapackage":"test 01","packageid":4}]
答案 0 :(得分:1)
正如我评论的那样,这可能是因为ASP.NET MVC没有将查询字符串的JSON部分反序列化为对象。您可以将参数更改为string
并自行反序列化(此示例使用JSON.NET):
public JsonResult ListaGenerica(string param, /* etc */)
{
DataTables dataTables = JsonConvert.DeserializeObject<DataTables>(param);
}
或者您可以使用POST
代替DataTables
结尾:
"fnServerData": function (sSource, aoData, fnCallback, oSettings) {
oSettings.jqXHR = $.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
});
}
有关documentation page的更多信息,用于提出服务器端请求。