【问题标题】:How to get data from C# WebMethod as Dictionary<> and display response using jquery ajax?如何从 C# WebMethod 获取数据作为 Dictionary<> 并使用 jquery ajax 显示响应?
【发布时间】:2016-05-11 12:15:56
【问题描述】:

这是我要返回的返回类型Dictionary&lt;ReportID, ReportDatail&gt; 其中类的结构为:

Class ReportDetail
{
    ReportID,
    ReportName,
    ReportShortName,
    List<ReportFields>
}

Class ReportFields
{
    SystemName,
    DBName,
    DataType,
    MaxLength,
    DefaultValue
}

我不知道如何将该响应作为字典返回。

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

}

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

如何将数据传递给WebMethod 并将响应返回为来自WebMethod 的字典

【问题讨论】:

  • 你是这个意思? return new Dictionary&lt;ReportID, ReportDatail&gt;(); ?目前还不清楚这里有什么问题。
  • 有点困惑。您是否要返回 Dictonary ,但 ReportID 不是类型。实际上,它是 ReportDetail 类的一个属性。我猜你的意思是 Dictionary 其中 T 是 ReportID 类型,不是吗?
  • 我已经存储了 Dictionary Result 并希望返回 Dictionary 其中键值作为 ReportDetails 的 ReportID..
  • @David,是的,我想返回 Dictionary 作为键 ReportID 表示 Dictionary 中与之关联的 ReportType..!
  • @Kaishu:好的。我想问题是......是什么阻止了你?

标签: jquery asp.net ajax webmethod


【解决方案1】:

数组的反序列化不支持“System.String”类型。

我不清楚为什么这段代码会给出这个错误信息。但是无论如何,您也许可以简化其中的一些序列化。由于该方法只需要一个字符串,因此只需给它一个以预期参数名称作为键的字符串。我将假设您的 JavaScript 代码中的 AccountType 是一个字符串:

data: { AccountItem: AccountType }

我不知道如何返回 Dictionary respose

同样的方式你会返回任何东西。因此,例如,如果您想返回 Dictionary&lt;int, ReportDetail&gt;,您可以这样做:

[WebMethod]
public static Dictionary<int, ReportDetail> GetReportDetail(string AccoutItem)
{
    return new Dictionary<int, ReportDetail>();
}

至于你将如何填充该对象实际数据(而不是仅仅返回一个空字典),这完全取决于你。

并在上面使用 jquery 进行处理

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

success: function (data) {
    for (var i = 0; i < data.length; i++) {
        // do something with data[i]
    }
}

【讨论】:

  • 实际问题出在成功函数中,How to access values from "data" in response for different properties in Dictionary i.e. ReportDetails properties and ReportFields properties ?
  • @Kaishu:你试过什么?它以什么方式不起作用?使用浏览器的调试工具检查 Web 服务返回的 JSON 数据的结构。例如,考虑到我在此答案中使用的人为代码,它可能类似于data[i].ReportName。但是,即使您进行一些粗略的调试也会确认或纠正这一点。
【解决方案2】:

试试这个:

定义要发送的类类型:

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

[WebMethod] 你需要像这样通过这个类:

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

}

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 2021-01-21
    • 2013-03-04
    • 1970-01-01
    • 2011-12-30
    • 2021-11-15
    相关资源
    最近更新 更多