【问题标题】:Cannot print data from a website using EF, LINQ into a list view无法使用 EF、LINQ 将网站中的数据打印到列表视图中
【发布时间】:2013-01-20 00:50:43
【问题描述】:

我有一个使用 EF 构建 DB 和 LINQ 来访问该数据的网站。我正在尝试将数据打印到列表视图中,但没有打印任何内容,我不知道为什么。

标记:

<asp:ListView ID="lvSearchResults" OnPagePropertiesChanged="lvSearchResults_PagePropertiesChanged" runat="server">
    <LayoutTemplate>
        <table style="width: 100%" border="1">
            <tr style="text-align:center;">
                <td>Item Name</td>
                <td>Release Region</td>
                <td style="width: 100px">Factory Sealed</td>
                <td style="width: 50px">Item Details</td>
                <td style="width: 50px">Edit Item</td>
            </tr>

            <tr id="itemPlaceHolder" runat="server" />
        </table>

        <asp:DataPager ID="dpSearchResults" PagedControlID="lvSearchResults" PageSize="10" runat="server">
            <Fields>
                <asp:NextPreviousPagerField ButtonType="Button" ShowNextPageButton="false" ShowFirstPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="true" />
                <asp:NumericPagerField ButtonCount="10" />
                <asp:NextPreviousPagerField ButtonType="Button" ShowNextPageButton="true" ShowFirstPageButton="false" ShowLastPageButton="true" ShowPreviousPageButton="false" />
            </Fields>
        </asp:DataPager>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%#:Item.Name%>
            </td>
            <td>
                <%#:Item.Region%>
            </td>
            <td style="text-align: center;">
                <%#:Item.Condition%>
            </td>
            <td>
                <asp:Button ID="btnSelect" Text="View Details" PostBackUrl="/Items/<%#:ItemName%>" runat="server" />
            </td>
            <td>
                <asp:Button ID="btnEdit" Text="Edit Item" PostBackUrl="/Edit/<%#:ItemName%>" runat="server" />
            </td>
        </tr>
    </ItemTemplate>
</asp:ListView>

代码隐藏:

byte SelectedType = byte.Parse(ddlCategories.SelectedValue);

using (var db = new FullContext())
{
    var q = (from i in db.Items
             where i.TypeID == SelectedType || SelectedType == 0 //Get certain type or all types
             orderby i.Name
             select new
             {
                 i.ID,
                 i.Name,
                 i.Region,
                 i.Condition
             }).ToList();
    lvSearchResults.DataSource = q;
    lvSearchResults.DataBind();
}

【问题讨论】:

    标签: c# asp.net .net linq-to-entities entity-framework-5


    【解决方案1】:

    解决方案是添加 Eval() 命令,以便工作代码

                                   <td>
                                    <%#:Eval("Name")%>
                                </td>
    
                                <td>
                                    <%#:Eval("Region")%>
                                </td>
    
                                <td style="text-align: center;">
                                    <%#:Eval("Condition")%>
                                </td>
    

    【讨论】:

      猜你喜欢
      • 2021-02-20
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 2020-02-10
      • 1970-01-01
      相关资源
      最近更新 更多