获取错误,同时尝试使用 API 并在 ASP.NET Webform 的 gridview 中显示内容

问题描述 投票:0回答:0

错误:目标类型 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
方法:

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

这是我的JSON部分(到目前为止,我已经输入了4个id的数据。这个JSON很长,有72个id)

{
"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}]"
}
c# asp.net-web-api webforms aspxgridview
© www.soinside.com 2019 - 2024. All rights reserved.