gpt4 book ai didi

javascript - 将 jQuery 数组发送到 MVC Controller

转载 作者:行者123 更新时间:2023-12-03 11:59:37 24 4
gpt4 key购买 nike

我通过 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;}
}

Controller :

[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}]

最佳答案

正如我所评论的,这可能是因为 ASP.NET MVC 没有将查询字符串的 JSON 部分反序列化为对象。您可以将参数更改为字符串并自行反序列化(本示例使用 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 的更多信息用于发出服务器端请求。

关于javascript - 将 jQuery 数组发送到 MVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25474339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com