我是Asp.Net Mvc5以及c#的新手,我希望在这里获得一些指示,我一直在试图弄清楚为什么我收到以下错误:
传递到字典中的模型项的类型是' System.Data.Entity.DbSet 1[SoccerTeams.Models.Player]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [SoccerTeams.Models.ViewModels.TeamViewModel]'。
但是现在调试后我发现ViewModel对象实际上是为所有项返回空值。我创建了一个可以正常添加团队以及该团队的玩家的页面。在我的数据库中,球队有一个桌子,所有球员都在另一个桌子上,每个球员都有球队的名字(球员和球队名称在两个单独的栏目中),因此它可以与正确的球队联系。我创建了一个ViewModel,我试图调用ViewModel并能够将所有玩家都返回到视图中,这样我就可以在列表中显示它们。
我的团队控制器用于"查看所有玩家视图"如下:
public ActionResult ViewAllPlayers()
{
TeamViewModel teamView = new TeamViewModel();
return View(teamView);
}
我的团队模型如下:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SoccerTeams.Models
{
// This teams class will be used to represent the teams in the database
public class Team
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string TeamName { get; set; }
public string Coach { get; set; }
public string Conference { get; set; }
}
}
我的玩家模型如下:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SoccerTeams.Models
{
public class Player
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public Guid TeamId { get; set; }
public string Name { get; set; }
}
}
我的ViewModel如下:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SoccerTeams.Models.ViewModels
{
public class TeamViewModel
{
public string TeamName { get; set; }
public string Coach { get; set; }
public string Conference { get; set; }
public List<Player> Players { get; set; }
}
}
我的CreateTeam操作如下:
public async Task<ActionResult> Create(TeamViewModel model, string addfiverows)
{
if (ModelState.IsValid)
{
if (!string.IsNullOrEmpty(addfiverows)) return View(model);
var team = new Team { TeamName = model.TeamName, Coach = model.Coach, Conference = model.Conference };
db.Teams.Add(team);
var result = await db.SaveChangesAsync();
if(result > 0)
{
foreach(var player in model.Players)
{
var p = new Player { Name = player.Name, Id = team.Id };
db.Players.Add(p);
}
result = await db.SaveChangesAsync();
}
if(result > 0) return RedirectToAction("Index");
}
return View();
}
我想要显示的我的视图如下:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SoccerTeams.Models.ViewModels.TeamViewModel
@{
ViewBag.Title = "View All Players";
}
<h2>View All Players</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.TeamName)
</th>
<th>
@Html.DisplayNameFor(model => model.Players)
</th>
<th></th>
</tr>
@foreach (var item in Model.Players)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.TeamName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Players)
</td>
</tr>
}
</table>
从研究开始,我认为这个问题与@model IEnumerable<SoccerTeams.Models.ViewModels.TeamViewModel>
有关。然而,在与@Tony Bao交谈后,我发现了我的理解,我注意到Viewmodel实际上是返回字段但是使用空值。
我也在寻找任何指南或教程,因为我不仅要寻找解决方案,还要更好地理解为什么会发生这种情况以及如何正确使用ViewModel。
答案 0 :(得分:3)
首先将Player
集合添加到Team
模型:
public class Team
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string TeamName { get; set; }
public string Coach { get; set; }
public string Conference { get; set; }
public virtual ICollection<Player> Players { get; set; }
}
并在Player
模型中添加Team
:
public class Player
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string Name { get; set; }
public Guid TeamId { get; set; }
public virtual Team Team {get; set;}
}
和ViewAllPlayers
行动:
public ActionResult ViewAllPlayers(Guid id)
{
var team = db.Teams.Include(t => t.Players).Single(t => t.Id == id);
TeamViewModel teamView = new TeamViewModel
{
TeamName = team.TeamName,
Coach = team.Coach,
Conference = team.Conference,
Player = new List<Player>(team.Players)
};
return View(teamView);
}
答案 1 :(得分:2)
您可以在方案中查看DisplayFor Templates。在displaytemplates(用于显示)和editortempates(用于插入和更新)上有大量材料。你可以在谷歌上搜索
我将在此提出建议
public ActionResult ViewAllPlayers()
{
//this should be from your database
var teamviewer = new TeamViewModel();
teamviewer.TeamName = "t1";
teamviewer.Players = new List<Player>() { new Player { PlayerName = "p1" }, new Player { PlayerName = "p2" } };
return View();
}
您的视图模型
public class TeamViewModel
{
public string TeamName { get; set; }
public string Coach { get; set; }
public string Conference { get; set; }
public List<Player> Players { get; set; }
}
public class Player
{
public string PlayerName { get; set; }
}
在此处创建一个Displayfor模板
Views/Shared/DisplayTemplates/Player.cshtml
Player.cshtml
@model SoccerTeams.Models.Player
@Html.DisplayFor(m=>m.PlayerName)
在您的索引页面
@model SoccerTeams.Models.TeamViewModel
<h2>View All Players</h2>
<table class="table">
<tr>
<td>
@Html.DisplayFor(modelItem => modelItem.TeamName)
</td>
<td>
@Html.DisplayFor(modelItem=>modelItem.Players)
</td>
</tr>
</table>