2

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

tereško
  • 58,060
  • 25
  • 98
  • 150
JMon
  • 3,387
  • 16
  • 63
  • 102

1 Answers1

0
where t.LeagueTeams.Any(lt => lt.LeagueID == tl.League.LeagueID)

Edit:

    var _Teams = (from t in db.Teams
                  from tl in t.LeagueTeams
                  select tl.League.Name);
usr
  • 168,620
  • 35
  • 240
  • 369
  • EXCELLENT that works, and how can I call it from the View then? At the moment I have @foreach (var item in Model.Teams) { @Html.DisplayFor(modelItem => item.TeamName) @Html.DisplayFor(modelItem => item.LeagueTeams) } – JMon Jan 25 '12 at 22:21
  • Not sure what you mean. I recommend you get all the data you need in the controller. That is usually a much saner approach than querying in the view. – usr Jan 25 '12 at 22:23
  • Yes I am trying to get everything in the controller, but I cannot seem to get the LeagueName. How can I display the LeagueName in the View? – JMon Jan 25 '12 at 22:29
  • I added a new proposal. This might be what you want. – usr Jan 25 '12 at 22:32
  • still a problem :( Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)..........Cannot remove the IEnumerable since then the View will not work – JMon Jan 25 '12 at 22:38
  • Yeah my query returns just the name because it is sample code. You need to think through this yourself because only you know precisely what you want to accomplish. I just try to put the right tools in your hand. – usr Jan 25 '12 at 23:05
  • Thanks for your help usr, I will take it from here – JMon Jan 26 '12 at 07:46