1

我对使用实体框架和 Linq 还很陌生。当从页面顶部的下拉列表中选择“区域”时,我在 web 表单中有一个 ListView,我想填充它。数据将基于下拉列表中选择的内容。我使用 EF 填充下拉列表的选项,我的挑战是让页面在页面回发时从 selectedValue 填充数据。我到了页面发回的地步,它返回“没有返回数据”,这是我在 EmptyDataTemplate 中的文本。在某处它没有检索我的数据,所以我希望有人能告诉我我做错了什么。

<section class="featured" style="text-align:center; background-color: #fff; padding:15px">
                <asp:Label ID="lblChooseArea" runat="server" Text="Choose an area: "></asp:Label>
                <asp:DropDownList ID="AreaDropDown" 
                    ItemType="Level1One.Classes.Area" AutoPostBack="true"
                    runat="server" DataTextField="AreaName"
                    SelectMethod="GetAreas" >                  
                </asp:DropDownList>
                <br /><hr /><br />

                <ul> 
                        <asp:ListView ID="ticketList" runat="server"                              
                            GroupItemCount="3"  
                            ItemType="Level1One.Classes.Ticket" >                            
                            <EmptyDataTemplate>      
                                <table id="Table1" runat="server" >        
                                    <tr>          
                                        <td>No data was returned.</td>        
                                    </tr>     
                                </table>  
                            </EmptyDataTemplate>  
                            <EmptyItemTemplate>     
                                <td id="Td1" runat="server" />  
                            </EmptyItemTemplate>  
                            <GroupTemplate>    
                                <tr ID="itemPlaceholderContainer" runat="server">      
                                    <td ID="itemPlaceholder" runat="server"></td>    
                                </tr>  
                            </GroupTemplate>  
                            <ItemTemplate>    
                                <td id="Td2" runat="server">      
                                    <table>        
                                        <tr>          
                                            <td>&nbsp;</td>                                            
                                            <td>
                                                <a href="ProductDetails.aspx?ticketID=<%#:Item.TicketID%>">
                                                    <span class="ProductName">
                                                        <%#:Item.TicketSubArea%>
                                                    </span>
                                                </a>            
                                                <br />
                                                <span class="ProductPrice">           
                                                    <b>Description: </b><%#:Item.Description%>
                                                </span>
                                                <br />            
                                            </td>        
                                        </tr>      
                                    </table>    
                                </td>  
                            </ItemTemplate>  
                            <LayoutTemplate>    
                                <table id="Table2" runat="server">      
                                    <tr id="Tr1" runat="server">        
                                        <td id="Td3" runat="server">          
                                            <table ID="groupPlaceholderContainer" runat="server">            
                                                <tr ID="groupPlaceholder" runat="server"></tr>          
                                            </table>        
                                        </td>      
                                    </tr>      
                                    <tr id="Tr2" runat="server"><td id="Td4" runat="server"></td></tr>    
                                </table>  
                            </LayoutTemplate>
                        </asp:ListView>
                </ul>

            </section>

这是后面的代码:

public partial class TicketList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {

            string dllArea = AreaDropDown.SelectedValue;
            int dllAreaNum;

            int.TryParse(dllArea, out dllAreaNum);

            using (TicketContext adrop = new TicketContext())
            {
                var result = (from a in adrop.Tickets
                              where (a.AreaID == dllAreaNum)
                              select new { a.TicketSubArea, a.Description, a.TicketID }).ToList();

                ticketList.DataSource = result;
                ticketList.DataBind();

            }
}   

    }        

    public IQueryable<Area> GetAreas()
    {
        var db = new Level1One.Classes.TicketContext();
        IQueryable<Area> query = db.Areas;
        return query;
}
}

我的课程和背景:

using System.Data.Entity;

namespace Level1One.Classes
{
    public class TicketContext : DbContext
    {
        public TicketContext() : base("Level1One")
        {
        }
        public DbSet<Ticket> Tickets { get; set; }
        public DbSet<Area> Areas { get; set; }
    }
}

.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace Level1One.Classes
{
    public class Ticket
    {
        [ScaffoldColumn(false)]
        public int TicketID { get; set; }

        [Required, StringLength(100), Display(Name = "Area")]
        public string TicketArea { get; set; }

        [Required, StringLength(100), Display(Name = "SubArea")]
        public string TicketSubArea { get; set; }

        [Required, StringLength(10000), Display(Name = "Description"), DataType(DataType.MultilineText)]
        public string Description { get; set; }

        public int? AreaID { get; set; }

        public virtual Area Area { get; set; }
    }
}
4

1 回答 1

0

添加DataValueField = AreaID到您的区域下拉列表

将此替换为

 if (IsPostBack)
        {

            string dllArea = AreaDropDown.SelectedValue;
            int dllAreaNum;

            int.TryParse(dllArea, out dllAreaNum);

            using (TicketContext adrop = new TicketContext())
            {
                var result = (from a in adrop.Tickets
                              where (a.AreaID == dllAreaNum)
                              select new { a.TicketSubArea, a.Description, a.TicketID }).ToList();

                ticketList.DataSource = result;
                ticketList.DataBind();

            }

和:

                if (IsPostBack)
                {

                    string dllArea = AreaDropDown.SelectedValue;
                    int dllAreaNum;

                    int.TryParse(dllArea, out dllAreaNum);

                       IQueryable<Ticket> result = getTicketbyArea(dllAreaNum)


                        ticketList.DataSource = result.ToList();
                        ticketList.DataBind();
        }
   }

删除这个:

        using (TicketContext adrop = new TicketContext())
        {
            var result = (from a in adrop.Tickets
                          where (a.AreaID == dllAreaNum)
                          select new { a.TicketSubArea, a.Description, a.TicketID }).ToList();

            ticketList.DataSource = result;
            ticketList.DataBind();

        }

将此方法添加到您将在页面加载时调用的代码中。

public IQueryable<Ticket> getTicketbyArea(int dllAreaNum)
{
     var _db = new TicketContext();

     var query = _db.Tickets
                    .Where(x => x.AreaID== dllAreaNum).Select(x =>x)
                  return query;

}
于 2013-03-11T20:21:53.653 回答