C # Receiving Serialized Objects in a WebMethod

4

Good afternoon, I am passing a list of objects from JS to C #, to a WebMethod, as a function parameter, what kind of parameters should this be?

I created a class to receive the data:

public class serializeItens {
    //Dados de itens
    public RowGrid [] sXmlItens { get; set; }

}

public class RowGrid {

    public decimal ItemID { get; set; }
    public string itemName { get; set; }
    public string itemUnid { get; set; }
    public decimal itemQtd { get; set; }
    public decimal itemCust { get; set; }
    public string itemIL { get; set; }
    public string itemCentr { get; set; }
    public decimal itemSaldo { get; set; }

    public RowGrid() {
        ItemID = 0;
        itemName = string.Empty;
        itemUnid = string.Empty;
        itemQtd = 0;
        itemCust = 0;
        itemIL = string.Empty;
        itemCentr = string.Empty;
        itemSaldo = 0;

    }
}

With exactly the same arguments I'm going through in JS ..

var oItem = {
    Codig: 0,
    Desc: "",
    Unid: "KG",
    Quant: 0,
    Custo: 0,
    IL: "",
    Centro: "",
    Saldo: 0
}
itensList.push(oItem);

But when I call WebMethod via ajax:

[WebMethod(EnableSession = true)]
public static bool salvaReg(EstoqMov PoMov, serializeItens PsItens) {

Returns the following error message:

Sys.Net.WebServiceFailedException: The server method 'salvaReg' failed with the following error: System.InvalidOperationException-- The 'Gradual.Web.serializeItems' type is not supported for deserialization of an array. em>

    
asked by anonymous 09.04.2014 / 20:04

1 answer

4

Your javascript should be making the call the wrong way.

Assuming you are using jQuery, do so:

var oItem = {
    Codig: 0,
    Desc: "",
    Unid: "KG",
    Quant: 0,
    Custo: 0,
    IL: "",
    Centro: "",
    Saldo: 0
}
itensList.push(oItem);

$.ajax({
    type: 'POST',
    url: 'servico.asmx/salvaReg',
    data: JSON.stringify({ PoMov: oMov, PsItens: { sXmlItens: itensList } }),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (data) {
        // resultado
    }
});
    
09.04.2014 / 20:13