Here is return type I want to return Dictionary<ReportID, ReportDatail>
where structure of classes as:
Class ReportDetail
{
ReportID,
ReportName,
ReportShortName,
List<ReportFields>
}
Class ReportFields
{
SystemName,
DBName,
DataType,
MaxLength,
DefaultValue
}
I don't have any idea about how to return that response as dictionary.
function GetReportsDetails(AccoutType) {
$.ajax({
type: "POST",
url: '<%= ResolveUrl("~/Web/ReportPosition.aspx/GetReportDetail") %>',
contentType: "application/json; charset=utf-8",
datatype: 'json',
data: JSON.stringify({SelectedAccount: AccoutType}),
success: function (data) {
alert(data.d);
},
error: function (xhr, status, error) {
alert('XHR: ' + xhr.responseText + '\nStatus: ' + status + '\nError: ' + error);
}
});
[WebMethod]
public static string GetReportDetail(string AccoutItem)
{
return "Response from WebMethod..!";
//What type of code I've to write here to return "Dictionary<ReportID, ReportDatail>"
}
In above web method I have simply return string rather than Dictionary as response but still gets error as:
Type \u0027System.String\u0027 is not supported for deserialization of an array.
How to pass data to WebMethod
and process response return as Dictionary from WebMethod
Type "System.String" is not supported for deserialization of an array.
It's not clear to me why this code is giving this error message. But you may be able to simplify some of this serialization anyway. Since the method is just expecting a string, give it just a string with the expected parameter name as the key. I'm going to assume that AccountType
in your JavaScript code is a string:
data: { AccountItem: AccountType }
I don't know how to return Dictionary<> respose
Same way you'd return anything. So, for example, if you want to return a Dictionary<int, ReportDetail>
you could do this:
[WebMethod]
public static Dictionary<int, ReportDetail> GetReportDetail(string AccoutItem)
{
return new Dictionary<int, ReportDetail>();
}
As for how you would populate that object with actual data (instead of just returning an empty dictionary), that's entirely up to you.
and process using jquery over that
When you return actual data, use your browser's debugging tools to examine the structure of the JSON response. It's really just an array of objects. You can loop over it, examine the properties of the objects, etc. just like any other objects.
success: function (data) {
for (var i = 0; i < data.length; i++) {
// do something with data[i]
}
}