I encountered a problem, while doing some MVC testing. So at the moment I have 2 tables, one called Leagues and one called Teams.
In Leagues I have LeagueID, LeagueName, and in Teams I have TeamID and TeamName. Then I have another table called LeagueTeams which contains the LeagueTeamID, fk_LeagueID and fk_TeamID.
Now in my ViewData, I am referencing the Teams table so I have something like the following :-
@foreach (var item in Model.Teams) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.TeamName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LeagueTeams)
</td>
</tr>
Now item.LeagueTeams is for example 1, which is the LeagueTeamID. How can I get the League Name in this code? Do I need to call a reference to something else, or create a linq statement?
UPDATE
I came up with this in my Controller but I still have a problem :-
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
TeamsData model = new TeamsData();
var _Teams = (from t in db.Teams
from tl in db.LeagueTeams
where t.LeagueTeams.Contains(tl.League.LeagueID)
select t);
model.Teams = _Teams;
return View(model);
}
However the code is throwing an error on tl.League.LeagueID, telling me :-
"cannot convert from 'int' to 'MyProject.Models.LeagueTeam'"
Any help would really be appreciated since I seem to be a bit lost here!
Thanks for your help and time