views:

106

answers:

4

hi, I am using jquery ajax method on my aspx page,which will invoke the webmethod in the code behind.Currently the webmethod takes a couple of parameters like firstname,lastname,address etc which I am passing from jquery ajax method using

data:JSON.stringify({fname:firstname,lname:lastname,city:city})

now my requirement has been changed such that,the number and type of parameters that are going to be passed is not fixed for ex.parameter combination can be something like fname,city or fname,city or city,lname or fname,lname,city or something else.So the webmethod should be such that it should accept any number parameters.I thought of using arrays to do so, as described here.

But I do not understand how can I identify which and how many parameters have been passed to the webmethod to insert/update the data to the DB.Please could someone help me with this?

thanks

A: 

You could return a single JSON string parameter and parse it on the server within your webmethod.

This is just one approach. Another might be to take a look at your overall design and think about how you might improve it so that you don't have to pass variable parameters around. they can be nasty, as you have found. Often, stepping back and refactoring can lead to a simpler, more elegant design that is a lot easier to program.

Daniel Dyson
sorry,could you please give me an example how can I identify which parameters have been passed from $.ajax to webmethod,in the webmethod to send it to the BLL?
kranthi
You only need to send a single string parameter to your webmethod. This string would contain the JSON serialized object that contains all of the 'parameters'. In the webmethod, you deserialize this JSON string and write logic to pass the resulting data to the BLL.
Daniel Dyson
A: 

You would just add the parameters to your object, depending on their availability/presence:

var params = {};
if(city != null) {
    params.city = city;
}
params.fname = $("#fname").val();
...etc...

$.ajax({
    ...
    data:JSON.stringify(params);
    ...
});
karim79
ok,what will be the signature of the webmethod?is it something like [webmethod]public static void create(string city,string fname,...){}?
kranthi
@kranthi - I thought you wanted to know how to send a variable number of parameters via jQuery. I really have no idea what the signature would look like.
karim79
ok,thanks.I actually want to know, how do I declare the webmethod and get the individual parameters from the list/array parameter that has been passed.
kranthi
A: 

I would pass the webmethod a string in specific format (maybe the format of your first example)

That way when you get the requeston the server you first split it on "," to get an array of name value pairs and then split each pair to get the individual name and value parts.

Keeps the call simple and you can handle it with complex code on the server with the fluffy .Net frameworks intellisense to help you out.

KISS = Keep It Simple Stupid

My old music teacher used to live by that, I only realised why recently !!!!

Wardy
+1  A: 

You can use a complex type on the client side that matches an object on the server side that contains an array or several arrays of your parameters.

Very crude example

(server side)

//class declaration     
public class MyObject
{
      //values
      string[] Keys;
      string[] Values;

      //methods
      public void Add()
      {...} 
}

//web service - WebService.asmx
[WebMethod]
public void AddObject(MyObject NewObject)
{
     NewObject.Keys.Length = ...;
     NewObject.Add();
     ...
}

(client side)

var MyObject= { };

MyObject.Keys = { $("#key1").val(), $("#key2").val(), ... };
MyObject.Values = { $("#val1").val(), $("#val2").val(), ... };

var DTO = { 'MyObject' : MyObject};

$.ajax({
   type: "POST",
   contentType: "application/json; charset=utf-8",
   url: "WebService.asmx/AddObject",
   data: JSON.stringify(DTO),
   dataType: "json"
});

The above example will serialize your object on the client side and you will be able to access the object on the server side just as if you created it with new().

Dave