mtsys
mtsys

Reputation: 249

Send jQuery array to MVC Controller

I'm sending an array object via ajax and C # MVC can not understand. I've tried several ways and does not work. What am I doing wrong?

I already have and without JSON.stringify, and not work. Variable teste01 receives null.

Class of object:

public class Teste
{
    public int dataid {get;set;}
    public string datapackage { get; set;}
    public int packageid {get;set;}
}

Contoller:

[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 functions:

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>';}}
        ],
    });

Query Strings Parameters

sEcho:2
.
.
.
filtroID:
aController:EmpresaCarga
aController:EmpresaCarga
teste01:[{"dataid":1,"datapackage":"test","packageid":3},{"dataid":2,"datapackage":"test 01","packageid":4}]

Upvotes: 0

Views: 408

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126082

As I commented, this is probably because ASP.NET MVC isn't deserializing the JSON part of the query string into an object for you. You can either change the parameter to a string and deserialize yourself (this example uses JSON.NET):

public JsonResult ListaGenerica(string param, /* etc */)
{
    DataTables dataTables = JsonConvert.DeserializeObject<DataTables>(param);
}

Or you could use a POST instead from the DataTables end:

"fnServerData": function (sSource, aoData, fnCallback, oSettings) {
  oSettings.jqXHR = $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": fnCallback
  });
}

Lots more information on the documentation page for making server-side requests.

Upvotes: 1

Related Questions