bit of a newbie to mvc. i am having trouble with the following scenario:
I have a view with the following:
<tr>
<% foreach (var game in (IEnumerable<Game>)ViewData["Game"])
{ %>
<td>
<input type="checkbox" name="selectedObjects" value="<%=game.Id%>" />
</td>
<td>
<%=game.GMTDateTime%>
</td>
<td>
开发者_Python百科 <%=game.HomeTeamId%>
</td>
<td>
<%=game.AwayTeamId%>
</td>
<td>
<%=game.Venue%>
</td>
</tr>
All of the data displays OK except for HomeId and AwayId. These two fields are linked to another table called team and each Id in HomeTeamId and AwayTeamId relate to a record in the Team table and each id has a "Name". My problem is how do I display the "Name" from the Team table rather than the Id field shown.
The controller shows:
ViewData["Game"] = dc.Games.GetGames();
The query I am using in linq is:
public static IEnumerable<Game> GetGames(this Table<Game> source)
{
return from c in source
orderby c.GMTDateTime
select c;
}
Assuming you have defined the relationships in your schema, this should work:
<td>
<%=game.Team.Name%>
</td>
<td>
<%=game.Team1.Name%>
</td>
Because there are multiple FKs to the Team table, you will see two properties for Team, Team
and Team1
(or similar). You'll need to determine which is which by looking at the generated LINQ code, or by trial and error, i.e., observing the output.
Normally using partial classes I will make additional properties that expose the Team object(s) with better names that make it clear which FK they are for.
精彩评论