I'm fairly new to using entity framework and Linq. I have a ListView in a webform that I want to populate when an "area" is chosen from the dropdownlist at the top of the page. The data will be based on what is chosen in the dropdown. I used EF to populate the choices for the dropdown and my challenge has been to have the page populate the data from the selectedValue when the page posts back. I got to the point where teh page posts back and it returns "No data was returned", my text in EmptyDataTemplate. Somewhere it is not retrieving my data so I'm hoping someone can tell me what I'm doing wrong.
<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> </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>
Here is the code behind:
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;
}
}
My classes and Context:
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; }
}
}