0

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}]
6
  • What does "does not work" mean exactly? Are you seeing an error on the page? Is nothing happening at all? Is control ever reaching your controller action? Commented Aug 24, 2014 at 17:44
  • @AndrewWhitaker variable teste01, receives null.
    – mtsys
    Commented Aug 24, 2014 at 17:47
  • Does the ModelState contain any errors? Commented Aug 24, 2014 at 17:48
  • @AndrewWhitaker switching to List<Teste> teste01. ModelState Return erro: {"The parameter conversion from type 'System.String' to type 'FlexGestor.Controllers.AjaxController+Teste' failed because no type converter can convert between these types."}
    – mtsys
    Commented Aug 24, 2014 at 17:56
  • 1
    I think this is because DataTables is issuing a GET request and ASP.NET MVC does not deserialize JSON in this case. You could model bind to a string and manually deserialize the JSON in your controller action. Commented Aug 24, 2014 at 18:03

1 Answer 1

1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.