如何从C#WebMethod获取数据作为Dictionary<>和显示响应使用jQuery的AJAX? [英] How to get data from C# WebMethod as Dictionary<> and display response using jquery ajax?

查看:215
本文介绍了如何从C#WebMethod获取数据作为Dictionary<>和显示响应使用jQuery的AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想返回的返回类型Dictionary<ReportID, ReportDatail> 类的结构为:

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

}

在上面的web方法中,我只是返回字符串而不是Dictionary作为响应,但是仍然会收到如下错误: Type \u0027System.String\u0027 is not supported for deserialization of an array.

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.

如何将数据传递到WebMethod并将处理响应作为字典从WebMethod

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

推荐答案

数组反序列化不支持类型"System.String".

Type "System.String" is not supported for deserialization of an array.

我尚不清楚为什么此代码给出了此错误消息.但是您仍然可以简化一些序列化操作.由于该方法只需要一个字符串,因此只给它一个以期望的参数名称作为键的字符串.我将假设您的JavaScript代码中的AccountType是字符串:

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 }

我不知道如何返回Dictionary<> respose

I don't know how to return Dictionary<> respose

以相同的方式您将返回任何内容.因此,例如,如果要返回Dictionary<int, ReportDetail>,则可以执行以下操作:

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.

并在其上使用jquery进行处理

and process using jquery over that

返回实际数据时,请使用浏览器的调试工具检查JSON响应的结构.它实际上只是一个对象数组.您可以像遍历其他对象一样遍历它,检查对象的属性等.

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

这篇关于如何从C#WebMethod获取数据作为Dictionary&lt;&gt;和显示响应使用jQuery的AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆