遇到错误,尝试调用 API 并在 ASP.NET Webform 的 GridView 中显示内容。

huangapple go评论65阅读模式
英文:

Getting error, while trying consume API and display the contents in gridview of ASP.NET Webform

问题

> 错误: 目标类型 System.Collections.IEnumerable 不是值类型或非抽象类。参数名称: targetType。

我尝试使用以下的GridView和C#函数来调用Web API端点。但是我收到了这个错误。我将在这里也发布我的Json结构。

我正在学习.NET的阶段,任何帮助将不胜感激。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="False" AllowSorting="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" CssClass="table table-striped table-bordered">
    <HeaderStyle CssClass="header-style" />
    <Columns>
        <asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="PlantLineProcess" HeaderText="PlantLineProcess" SortExpression="PlantLineProcess" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="DefectLocation" HeaderText="Defect Location" SortExpression="DefectLocation" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Defect" HeaderText="Defect" SortExpression="Defect" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="NMR_Number" HeaderText="NMR Number" SortExpression="NMR_Number" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" ControlStyle-CssClass="text-center" />
    </Columns>
</asp:GridView>

这段代码来自于 `page_load` 方法:

```csharp
if (!Page.IsPostBack)
{
    try
    {
        string apiUrl = "https://itapps/nmrapi/api/nmr";

        HttpClient httpClient = new HttpClient();
        HttpResponseMessage response = await httpClient.GetAsync(apiUrl);
        string jsonData = await response.Content.ReadAsStringAsync();

        // 检查JSON数据是数组还是单个对象
        dynamic responseData = JsonConvert.DeserializeObject(jsonData);
        dynamic data;

        if (responseData is JArray)
        {
            data = responseData;
        }
        else
        {
            data = responseData.data;
        }

        // 将数据绑定到GridView
        DataTable table = new DataTable();
        table.Columns.Add("id", typeof(int));
        table.Columns.Add("Type", typeof(string));
        table.Columns.Add("PlantLineProcess", typeof(string));
        table.Columns.Add("DefectLocation", typeof(string));
        table.Columns.Add("Defect", typeof(string));
        table.Columns.Add("Description", typeof(string));
        table.Columns.Add("NMR_Number", typeof(string));
        table.Columns.Add("Quantity", typeof(int));
        table.Columns.Add("Status", typeof(string));
        table.Columns.Add("Date", typeof(string));

        foreach (var item in data)
        {
            table.Rows.Add(item.id, item.Type,
                           item.PlantLineProcess,
                           item.DefectLocation,
                           item.Defect,
                           item.Description,
                           item.NMR_Number,
                           item.Quantity,
                           item.Status,
                           item.Date);
        }

        GridView1.DataSource = data;
        GridView1.DataBind();
    }
    catch (Exception ex)
    {
        lblMessage.Text = "Error: " + ex.Message;
    }
}
英文:

> Error: Target type System.Collections.IEnumerable is not a value type or a non-abstract class. Parameter name: targetType.

I was trying to consume Web API endpoint with below gridview and C# function. But I'm getting this error. I will post my Json structure also here.

I'm learning phase of .NET and any help would be much appreciated.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="False" AllowSorting="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" CssClass="table table-striped table-bordered">
<HeaderStyle CssClass="header-style" />
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" ControlStyle-CssClass="text-center" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" ControlStyle-CssClass="text-center" />
<asp:BoundField DataField="PlantLineProcess" HeaderText="PlantLineProcess" SortExpression="PlantLineProcess" ControlStyle-CssClass="text-center" />
<asp:BoundField DataField="DefectLocation" HeaderText="Defect Location" SortExpression="DefectLocation" ControlStyle-CssClass="text-center" />
<asp:BoundField DataField="Defect" HeaderText="Defect" SortExpression="Defect" ControlStyle-CssClass="text-center" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" ControlStyle-CssClass="text-center" />
<asp:BoundField DataField="NMR_Number" HeaderText="NMR Number" SortExpression="NMR_Number" ControlStyle-CssClass="text-center" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" ControlStyle-CssClass="text-center" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ControlStyle-CssClass="text-center" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" ControlStyle-CssClass="text-center" />
</Columns>
</asp:GridView>

This code is from page_load method:

if (!Page.IsPostBack)
{
try
{
string apiUrl = "https://itapps/nmrapi/api/nmr";
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(apiUrl);
string jsonData = await response.Content.ReadAsStringAsync();
// Check if the JSON data is an array or a single object
dynamic responseData = JsonConvert.DeserializeObject(jsonData);
dynamic data;
if (responseData is JArray)
{
data = responseData;
}
else
{
data = responseData.data;
}
// Bind the data to the GridView
DataTable table = new DataTable();
table.Columns.Add("id", typeof(int));
table.Columns.Add("Type", typeof(string));
table.Columns.Add("PlantLineProcess", typeof(string));
table.Columns.Add("DefectLocation", typeof(string));
table.Columns.Add("Defect", typeof(string));
table.Columns.Add("Description", typeof(string));
table.Columns.Add("NMR_Number", typeof(string));
table.Columns.Add("Quantity", typeof(int));
table.Columns.Add("Status", typeof(string));
table.Columns.Add("Date", typeof(string));
foreach (var item in data)
{
table.Rows.Add(item.id, item.Type,
item.PlantLineProcess,
item.DefectLocation,
item.Defect,
item.Description,
item.NMR_Number,
item.Quantity,
item.Status,
item.Date);
}
GridView1.DataSource = data;
GridView1.DataBind();
}
catch (Exception ex)
{
lblMessage.Text = "Error: " + ex.Message;
}
}

This is my part of JSON (as of now, I have entered data for 4 ids alone. This JSON is long and having 72 ids)

{
"success":true,
"message":null,
"data":"[{\"id\":1,\"Type\":\"Neeraj\",\"SerialNo\":\"DFR45566678\",\"PlantLineProcess\":\"new jackpot\",\"Date\":\"0001-01-01T00:00:00\",\"DefectLocation\":\"First Corner\",\"Defect\":\"AOL\",\"Description\":\"my description\",\"NMR_Number\":\"7383\",\"Quantity\":10,\"Status\":0},{\"id\":2,\"Type\":\"Jack\",\"SerialNo\":\"BL11099035474\",\"PlantLineProcess\":\"new jackpot\",\"Date\":\"0001-01-01T00:00:00\",\"DefectLocation\":\"First Corner\",\"Defect\":\"AOL\",\"Description\":\"my description\",\"NMR_Number\":\"7383\",\"Quantity\":1,\"Status\":0},{\"id\":3,\"Type\":\"Jack\",\"SerialNo\":\"BL11099035474\",\"PlantLineProcess\":\"new jackpot\",\"Date\":\"0001-01-01T00:00:00\",\"DefectLocation\":\"First Corner\",\"Defect\":\"AOL\",\"Description\":\"my description\",\"NMR_Number\":\"7383\",\"Quantity\":1,\"Status\":0}]"
}

答案1

得分: 1

从你的 JSON 中,data 是一个字符串。

当你反序列化 jsonData 时,response.dataJValue 类型而不是 IEnumerable

你必须再次反序列化 response.data,可以使用以下任一方法:

data = JArray.Parse(responseData.data.ToString());

或者

data = JsonConvert.DeserializeObject(responseData.data.ToString());
英文:

From your JSON, data is a string.

When you deserialize jsonData, response.data is JValue type but not an IEnumerable.

You have to deserialize once more for response.data with either of these:

data = JArray.Parse(responseData.data.ToString());

Or

data = JsonConvert.DeserializeObject(responseData.data.ToString());

huangapple
  • 本文由 发表于 2023年3月7日 12:51:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定