I have 2 tables:
User(Id PK, TitleId FK, Name, Active)
Title(Id PK, Text)
Table Title is a lookup table of titles (Mr, Miss,…) and its got a relationship with table User by TitleId.
Now I’m trying to display the data in a web page using .net and Entity Framework. I’m using a ListView control and a query as a source. it all displays fine except for the Title.
Here is my code:
MyEntities _entities = new MyEntities ();
User user = new User(_entities);
IQueryable u = (from x in _entities.Users
where x.Active == true
select x);
ListView1.DataSource = u;
ListView1.DataBind();
and the code in the front end:
<asp:ListView ID="ListView1" runat="server"
EnableModelValidation="True" DataKeyNames="Id">
<ItemTemplate>
<tr>
<td>
<%# Eval("Id") %>
</td>
<td>
<%# Eval("Title.Text") %>
</td>
<td>
<%# Eval("Name") %>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table ID="itemPlaceholderContainer" runat="server">
<tr runat="server">
<th id="Th2" runat="server">
Id</th>
<th id="Th1" runat="server">
Title</th>
<th id="Th3" runat="server">
Name</th>
<th runat="server">
</tr>
<tr runat="server" ID="itemPlaceholder">
</tr>
</table>
</LayoutTemplate>
How do I manage to get the Title Text associated with a User to display on the ListView?
I just figured out how to do it:
_entities.Users.Include(“Title”)