2017-03-11 94 views
0

我已經在互聯網上搜索了2天,找到了我的問題的答案但沒有正確的答案。我如何顯示球員名單中的球員mvc

這些都是我的班

public class Player 
{ 
    public int ID { get; set; } 

    public string Name { get; set; } 

    public string FirstName { get; set; } 

    public Team Team { get; set; } 

    public int TeamId { get; set; } 

public class Team 
{ 
    public int TeamID { get; set; } 

    public string Name { get; set; } 

    public virtual List<Player> Players { get; set; } 

我加入我的ViewModels是相同的 - >

public class CreatePlayerVM 
{ 
    public int ID { get; set; } 

    [Display(Name = "Last name")] 
    public string Name { get; set; } 

    [Display(Name = "First name")] 
    public string FirstName { get; set; } 

    [ForeignKey("TeamId")] 
    public Team Team { get; set; } 

    [Required] 
    [ForeignKey("Team")] 
    [Display(Name = "Team")] 
    public int TeamId { get; set; } 

public class ListPlayerVM 
{ 
    public int ID { get; set; } 

    public string Name { get; set; } 

    public string FirstName { get; set; } 

    [ForeignKey("TeamId")] 
    public Team Team { get; set; } 

    public int TeamId { get; set; } 

和我的球員名單的看法是這樣的 - >

 @Html.DisplayNameFor(model => model.Name) 
     @Html.DisplayNameFor(model => model.FirstName) 
     @Html.DisplayNameFor(model => model.Team) 
     @Html.DisplayNameFor(model => model.YellowCard) 
     @Html.DisplayNameFor(model => model.RedCard) 
     @Html.DisplayNameFor(model => model.Goal) 

     @foreach (var item in Model) 
     @Html.DisplayFor(modelItem => item.Name) 
     @Html.DisplayFor(modelItem => item.FirstName) 
     @Html.DisplayFor(modelItem => item.Team.Name) 
     @Html.DisplayFor(modelItem => item.YellowCard) 
     @Html.DisplayFor(modelItem => item.RedCard) 
     @Html.DisplayFor(modelItem => item.Goal) 

和homeController

[HttpGet] 
    public ActionResult Players() 
    { 
     List<Player> p1 = Player.GetPlayers(); 

     List<ListPlayerVM> p2 = new List<ListPlayerVM>(); 

     foreach (Player p in p1) 
     { 
      p2.Add(new ListPlayerVM(p.ID, p.Name, p.FirstName, p.Team , p.TeamId, p.Goal, p.YellowCard, p.RedCard)); 
     } 

     return View(p2); 
    } 

和CreateController

[HttpPost] 
    public ActionResult CreatePlayer(CreatePlayerVM model) 
    { 
     if (ModelState.IsValid) 
     { 

      Player player = new Player(model.FirstName, model.Name, model.Team, model.TeamId ,0, 0, 0); 
      player.InsertPlayer(); 

     } 
     PopulateTeamsDropDownList(model.TeamId); 
     return View(model); 
    } 

如何我在創建玩家列表中顯示我的teamName?厭倦了通過互聯網搜索希望有人能幫助我!

https://i.stack.imgur.com/5ELCJ.jpg

public static Player GetPlayer(int id) 
    { 
     using (LeagueDBContext ctx = new LeagueDBContext()) 
     { 
      return ctx.Players.Where(a => a.ID == id).SingleOrDefault(); 
     } 
    } 

GRTS

+0

它不清楚是什麼你問這裏 - 你'@ Html.DisplayFor(modelItem => item.Team.Name)'將顯示球隊的名字(假設你的查詢正在加載它)。並且刪除你的視圖模型上的[[ForeignKey(「TeamId」)]'屬性(它們是EF特定的屬性,並且與視圖模型無關) –

+0

我已經編輯了帖子,屏幕顯示了我所得到的..它不顯示名稱。 – BooZzken

+0

然後'Team'的Name屬性不包含任何數據!您可能需要在查詢中使用'.Include(x => x.Team)',因爲您沒有在數據模型中將'Team'屬性標記爲虛擬(您沒有顯示您的'GetPlayers() '方法)。你的'CreatePlayer()'方法與你的問題有什麼關係? –

回答

1

的問題是,你不懶加載你的團隊的數據。你必須使用虛擬來延遲加載它。您的播放器模式應該是這樣的:

public class Player 
{ 
    public int ID { get; set; } 

    public string Name { get; set; } 

    public string FirstName { get; set; } 

    public virtual Team Team { get; set; } 

    public int TeamId { get; set; } 
}