-2

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

Kaishu
  • 377
  • 1
  • 7
  • 21
  • You mean this? `return new Dictionary();` ? It's not really clear what the problem is here. – David May 11 '16 at 12:23
  • Its a little bit confused. Do you want return a Dictonary , but ReportID is not a type. Actually, its a property of ReportDetail class. I guess you mean a Dictionary where T is ReportID type , isnt ? – Luty May 11 '16 at 12:23
  • I have stored Dictionary Result already and wants to return Dictionary where key values as ReportID of ReportDetails.. – Kaishu May 11 '16 at 12:27
  • @David, Yes I wants to return Dictionary as key `ReportID` indicates what ReportType associated with it in Dictionary..! – Kaishu May 11 '16 at 12:36
  • @Kaishu: Ok. I guess the question is... What's stopping you? – David May 11 '16 at 12:37
  • @David, First of all I don't know how to return `Dictionary<>` respose and process using jquery over that. And the second thing is that I simply passing string to WebMethod to check whether or not it calling WebMethod but for that I am getting error as mention above... – Kaishu May 11 '16 at 12:43

2 Answers2

1

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]
    }
}
David
  • 208,112
  • 36
  • 198
  • 279
  • Actual problem is in success function, `How to access values from "data" in response for different properties in Dictionary i.e. ReportDetails properties and ReportFields properties `? – Kaishu May 11 '16 at 12:59
  • @Kaishu: What have you tried? In what way is it not working? Use your browser's debugging tools to examine the structure of the JSON data being returned by the web service. It's *probably* something like `data[i].ReportName` for example, given the contrived code I used in this answer. But even some cursory debugging on your part will confirm or correct that. – David May 11 '16 at 13:02
0

Try this:

Define the class type to send:

public class DataAccountItem
{
    public string SelectedAccount { get; set; }
}

In the [WebMethod] you need pass this class like this:

    [WebMethod]
public static string GetReportDetail(DataAccountItem myItem)
{
    return "Response from WebMethod..!";
    //What type of code I've to write here to return "Dictionary<ReportID, ReportDatail>" 

}
MarPreSI
  • 21
  • 2