Is is possible to return two values from a WebService to jQuery.
I tried like
[WebMethod(EnableSession = true)]
public string testing(string testId)
{
string data = string.Empty;
string data1 = string.Empty;
List<test1> datalist1 = new List<test1>();
List<test> datalist = new List<test>();
//coding
data = jsonSerialize.Serialize(datalist1);
data1 = jsonSerialize.Serialize(datalist);
return [data,data1];
}
but its showing error….how can we return two values from webservice here…..
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Another way is to create a custom data type that has the two return values you want:
[Serializable]
public sealed class MyData
{
public string Data { get; set; }
public string Data1 { get; set; }
}
…
[WebMethod(EnableSession = true)]
public MyData testing(string testId)
{
string data = string.Empty;
string data1 = string.Empty;
List<test1> datalist1 = new List<test1>();
List<test> datalist = new List<test>();
//coding
data = jsonSerialize.Serialize(datalist1);
data1 = jsonSerialize.Serialize(datalist);
return new MyData { Data = data, Data1 = data1 };
}
OR
[Serializable]
public sealed class MyData
{
public List<test> Data { get; set; }
public List<test1> Data1 { get; set; }
}
…
[WebMethod(EnableSession = true)]
public string testing(string testId)
{
MyData data = new MyData();
string alldata = string.Empty;
List<test1> datalist1 = new List<test1>();
List<test> datalist = new List<test>();
//coding
data.Data = datalist1;
data.Data1 = datalist;
alldata = jsonSerialize.Serialize(data);
return alldata;
}
Method 2
Well you could return an array of strings:
public string[] Testing(...)
{
return new string[] { data, data1 };
}
You’d then need to perform each bit of JSON deserialization separately on the client, which isn’t likely to be terribly pleasant 🙁
Method 3
return [data, data1] is not valid C# syntax. If you want to return a JSON array to the caller, use the JsonSerializer to serialize the array and return the resulting string.
Method 4
a webmethod is like a regular method, it can only return one value.
if its the same type you can use a list or array.
if its different types you can make a class holding the data
Method 5
I tried like
return jsonSerialize.Serialize(new { list1 = datalist1, list = datalist });
and I can receive these from my jQuery file easily. Thanks everyone for ur support
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0