5

Actually I have a Array declared on JS side like below:

 var benefArray = {};
 var benefCount = 0;
 var benefNome = $('#txtBenefNome').val();
 var benefDataNasc = $('#txtBenefDataNasc').val();
 var benefGrauParent = $('#txtBenefGrauParent').val();

 benefCount++;
 benefArray[benefCount] = new Array(benefNome, benefDataNasc, benefGrauParent);

              //Ajax Sender
            function sendAjax(url, parametros, sucesso) {
                $.ajax({
                    type: "POST",
                    url: url,
                    data: parametros,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: sucesso
                });
            };

 sendAjax("Client.aspx/AddClient", "{benefArray: \"" + benefArray + "\"}",
 function (msg) {
                    var retorno = msg.d;
                    alert(retorno);
                });

On my C# WebMethod side I Have:

    [WebMethod]
    public static string AddClient(object benefArray)
    {
        var t = benefArray;
    }

I'm trying to get those values from Javascript, what I have to do? Any insight on this will be appreciated! Thanks

1
  • 1
    benefArray isn't an array. It's an object. Each of the members you're assigning to it (benefArray[benefCount] = new Array(benefNome, benefDataNasc, benefGrauParent);) is an array. Granted the distinction is a bit subtle in JavaScript as JavaScript arrays aren't really arrays, but serializers will tend to look at the type to see if it's an array when making serialization decisions, so the distinction could make a difference to what you're doing... Commented Jun 19, 2011 at 16:33

2 Answers 2

11

Start by defining a model that will represent the data you are working with so that you work with strong types and get rid of the object ugliness on your AddClient method:

public class Benef
{
    public string Nome { get; set; }
    public string DataNasc { get; set; }
    public string GrauParent { get; set; }
}

then have your web method take an array of this model:

[WebMethod]
public static string AddClient(Benef[] benefs)
{
    // TODO: process ...

    // by the way as a result you could also return a strongly 
    // typed model and not only strings 
    // which could be easily manipulated on the client side
    return "some result"; 
}

and on the client you would define an array of parameters:

var parameters = { 
    benefs: [
        {
            Nome: $('#txtBenefNome').val(),
            DataNasc: $('#txtBenefDataNasc').val(),
            GrauParent: $('#txtBenefGrauParent').val()
        }
    ]
};

$.ajax({
    type: 'POST',
    url: 'Client.aspx/AddClient',
    data: JSON.stringify(parameters),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(result) {
        alert(result.d);
    }
});

As far as the JSON.stringify method I am using here is concerned it is native in modern browsers. But if you intend to support older browsers it is recommended to include the json2.js script to your page.

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for the workaround Darin! I'll try to do this and give you a feedback.
@FelipeKM, of course you can, on the client side simply do this: var parameters = { benefs: [ { ... }, { ... }, { ... } ] }; where you would obviously replace the ... by the details of each benef.
@FelipeKM, I get your point. You can push new elements to the array: parameters.benefs.push({ Nome: 'foo', DataNasc: 'bar', GrauParent: 'baz' });.
@FelipeKM, the .push method allows you to add elements to an existing array.
Thank you for a COMPLETE answer, with full code example for all portions of the process. I have looked at a great deal of posts where one portion of the process or another was just brushed over and not explained.
|
0

If you want to avoid setting up a class, you can also do this

[WebMethod]
public static string AddClient(Hashtable[] benefs)
{       
    var n = benefs["Nome"].ToString();
    return "some result"; 
}

Other types would require .ToString() and then Parse().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.