C# Receiving Serialized Objects on a Webmethod

Asked

Viewed 1,406 times

4

Good afternoon, I am passing a list of JS objects to C#, to a Webmethod, as function parameter, what kind of parameters this should 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 the 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 'salvaReg' server method failed with the following error: System.Invalidoperationexception-- The 'Gradual.Web.serializeItens' type is not supported for deserialization of an array.

  • How is your AJAX call being made? You could post the code?

  • Sure.. Pagemethods.salvaReg(oMov, itensList, salvaReg_cb);

  • It would have to be the ajax call itself: $.ajax(...

1 answer

4

Your javascript must be making the call the wrong way.

Assuming you’re 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
    }
});
  • It’s not a service, it’s a Webmethod...

  • 1

    Is from WebMethod same that I’m talking about, when I say service URL... but I’ll put an example, fictitious, to be clear.

  • Much better :) +1

  • @Ciganomorrisonmendez: it was William de Castro who asked the question... but so far he has not expressed himself.

  • @Williamdecastro Does the answer meet your expectations? If yes, please mark it as accepted for the benefit of the community. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.