使用C#设置为数据绑定列表视图(Setting up databind for a listview

2019-09-29 05:40发布

我是相当新的使用实体框架和LINQ。 我有我想要当从下拉列表页面的顶部选择“区域”来填充一个WebForm一个ListView。 这些数据将基于什么在下拉列表选择。 我用EF来填充下拉菜单的选择,我的挑战是在页面从了selectedValue当页面回填充数据。 我得到的地步TEH页面回并返回“无数据返回”,我在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; }
    }
}

Answer 1:

添加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;

}


文章来源: Setting up databind for a listview using c#