views:

105

answers:

3

I'm relatively new to utilizing web services. I'm trying to create one that will be accepting data from a ASP.Net form whose input controls are created dynamically at runtime and I don't how many control values will be getting passed.

I'm thinking I'll be using jQuery's serialize() on the form to get the data, but what do I have the web service accept for a parameter? I thought maybe I could use serializeArray(), but still I don't know what type of variable to accept for the JavaScript array.

Finally, I was thinking that I might need to create a simple data transfer object with the data before sending it along to the web service. I just didn't wanna go through with the DTO route if there was a much simpler way or an established best practice that I should follow.

Thanks in advance for any direction you can provide and let me know I wasn't clear enough, or if you have any questions.

A: 

Maybe this article is helpful (since you are using jQuery)

renick
A: 

The answer to the headline question (assuming this is an ASP.Net web service) is to use the params keyword in your web service method:

[WebMethod]
public void SendSomething(params string[] somethings)
{
    foreach (string s in somethings)
    {
        // do whatever you're gonna do
    }
}

Examples:

SendSomething("whatever");
SendSomething("whatever 1", "whatever 2", "whatever 3");

In fact, you don't really even need the params keyword - using an ordinary array as a parameter will let you pass in an unknown number of values.

MusiGenesis
I need the control ID and the value as a key value pair, so I'm trying to pass the JSON string of the form using var postData = $('#aspnetForm').serializeArray(); and then $.ajax({ data: JSON.stringify(postData). And setting the web service to accept a string array errorred with "Type System.Collections.Generic.IDictionary`2[[System.String, ... is not supported for deserialization of an array.". So I thought I probably needed a multi-dimensional array and changed the web service's parameter to string[,] and still received the same exception.
Billyhole
Am I correct in assuming that you're trying to call the web service from the client page via AJAX? Is the form too large to do a regular postback?
MusiGenesis
You are correct. I am trying to not do a postback since this is basically, a more complex input validation, but to the user, still just input validation.
Billyhole
Your problem is probably in the way you're calling the web service from AJAX, then. Whenever a C# client calls a web service, the serialization/deserializition grisliness is completely hidden, and you can pass in arrays of objects or almost anything (even List<string>) as parameters. I'm pretty sure you can also pass string[], but maybe try defining your web service method to take object[] and see what that does.
MusiGenesis
Oop, this may be simpler than I thought. In the code in your first comment here, it looks like you're trying to call serializeArray() on something that is just a single string (i.e. not a string *array*).
MusiGenesis
I'm calling jQuery's serializeArray() on the entire ASP.Net form.
Billyhole
I don't know AJAX or JQuery at all, but it looks like JSON.stringify returns just a single string, so your web service method should be defined to take a string, not a string *array*. You'll then have to parse that string (to get out the key/value pairs) inside the web service method.
MusiGenesis
A: 

Well I went with creating my own data transfer object which I guess was always the front of brain solution, I was just thinking that there was probably a recognized best practice on how to handle this.

Billyhole