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

*爱你&永不变心* 提交于 2019-12-14 03:29:34

问题


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


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



回答2:


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>" 

}


来源:https://stackoverflow.com/questions/37162420/how-to-get-data-from-c-sharp-webmethod-as-dictionary-and-display-response-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!