无法使用EF,LINQ将数据从网站打印到列表视图中

时间:2013-01-20 00:50:44

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

我有一个使用EF构建数据库和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();
}

1 个答案:

答案 0 :(得分:0)

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

                               <td>
                                <%#:Eval("Name")%>
                            </td>

                            <td>
                                <%#:Eval("Region")%>
                            </td>

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