0

I need to output information from two tables

public class Team
{
    [Key]
    public int TeamId { get; set; } 
    public int TypeId { get; set; }
    public string TeamName { get; set; }


}public class TeamType
{
    [Key]
    public int TypeId { get; set; }
    public string TypeName { get; set; }

    public virtual ICollection<Team> Teams { get; set; } 
}

I build this class

public abstract class MyAppController : Controller
{
    //
    // GET: /MyApp/
    private RenegadeEntities db = new RenegadeEntities();

    public RenegadeEntities DataContext
    {
        get { return db; }
    }

    public MyAppController()
    {
        ViewBag.TeamList = db.TeamTypes.Include("Teams").ToList();
    }
}

in view i do following:

@using Renegades.Helpers
@model IEnumerable<Renegades.Models.TeamType>
@foreach (var t in ViewBag.TeamList)
                            {
                                <li>
                                    <span>@t.TypeName</span>
                                    <ul id="sub_team_menu">
                                     @foreach (var team in t.Team)
                                     {
                                         <li>team.TeamName</li>
                                     }
                                    </ul>
                                </li>
                            }

Please help me to understand how can i output data from two or more tables in my view

tereško
  • 58,060
  • 25
  • 98
  • 150

1 Answers1

0

If tables are connected to each other through foreign key like in your case then you can use other table as property. Like TeamType.Teams. But I see that you are not referencing the TeamType in your Team class.

Edit your class like:

public class Team
{
    [Key]
    public int TeamId { get; set; } 
    public int TypeId { get; set; }
    public string TeamName { get; set; }
    public virtual TeamType Type{get;set;}  // Add this to your class
}

Then use Team.Type to access Type table as property. Like:

 @foreach (var team in t.Team)
{
    <li>team.Type.TypeName</li>
}

But if the tables are not connected to each other then simplest answer is you have to create ViewModel for that. Now what is a ViewModel, that is not so simple, To study about ViewModel follow the link :

http://www.codeproject.com/Articles/687061/Using-Multiple-Models-in-a-View-in-ASP-NET-MVC-4

Here is another more simpler link:

http://sampathloku.blogspot.ae/2012/10/how-to-use-viewmodel-with-aspnet-mvc.html

Usman Khalid
  • 3,032
  • 9
  • 41
  • 66
  • When I use this contrustion in my controller teamModel=$db.TemType.Include("Teams").ToList(); and use following code in view @foreach(var t in Model.Teamtypes) – user3323401 Feb 18 '14 at 12:50
  • {
  • @t.TypeName
  • @foreach( var team in t.Teams){@t.TeamName} evrythin work but i don't whant to past this code in evre my controller – user3323401 Feb 18 '14 at 12:51
  • For that you have to come up with a proper architecture. – Usman Khalid Feb 18 '14 at 12:56