Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

asp.net - How to get data from C# WebMethod as Dictionary<> and display response using jquery ajax?

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 + '
Status: ' + status + '
Error: ' + 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.Stringu0027 is not supported for deserialization of an array.

How to pass data to WebMethod and process response return as Dictionary from WebMethod

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...