C# 从联接查询在视图中显示2个模型
我试图在视图中显示关联的查询结果,但不断出现以下错误: 传递到字典中的模型项的类型为 “System.Collections.Generic.ListC# 从联接查询在视图中显示2个模型,c#,sql,asp.net-mvc,asp.net-mvc-4,razor,C#,Sql,Asp.net Mvc,Asp.net Mvc 4,Razor,我试图在视图中显示关联的查询结果,但不断出现以下错误: 传递到字典中的模型项的类型为 “System.Collections.Generic.List1[f_uAnonymousType42[MvcFFL.Models.TmPlyrs,MvcFFL.Models.Player]”, 但此词典需要类型为的模型项 'System.Collections.Generic.IEnumerable'1[MvcFFL.Models.TmPlyrs]' 以下是从控制器返回的查询: var z = (from
1[f_uAnonymousType4
2[MvcFFL.Models.TmPlyrs,MvcFFL.Models.Player]”,
但此词典需要类型为的模型项
'System.Collections.Generic.IEnumerable'1[MvcFFL.Models.TmPlyrs]'
以下是从控制器返回的查询:
var z = (from y in db.Teamplayers
join p in db.Players on y.playerid equals p.Nid
select new { y, p }).ToList();
return View(z);
现在,我的视图显示数据正在返回到:
@model IEnumerable<MvcFFL.Models.TmPlyrs>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm("Index", "TeamPlayers", FormMethod.Get))
{
<text>Team </text>@Html.DropDownList("Tms", "All")
<input type="submit" value="Search" />
}
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.playerid)
</th>
<th>
@Html.DisplayNameFor(model => model.teamid)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@item.playerid
</td>
<td>
@item.teamid
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
@ActionLink(“新建”、“创建”)
@使用(Html.BeginForm(“Index”,“TeamPlayers”,FormMethod.Get))
{
Team@Html.DropDownList(“Tms”、“所有”)
}
@DisplayNameFor(model=>model.playerid)
@DisplayNameFor(model=>model.teamid)
@foreach(模型中的var项目){
@item.playerid
@item.teamid
@ActionLink(“编辑”,“编辑”,新的{id=item.id})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
}
如何使其返回结果而不出错?您不能将两个模型对象传递给视图,因为您的视图仅绑定到一个模型类,因此您需要创建一个视图模型类,该类将具有两个类的属性 假设您的模型如下所示:
public class TeamPlayers
{
public int Id {get;set;}
public int Name {get;set;}
public int Nid {get;set;}
..........................
..........................
}
public class Player
{
public int PlayerId {get;set;}
public int PlayerName {get;set;}
................................
................................
}
在项目中创建一个名为ViewModels的文件夹,并创建一个具有两个模型属性的ViewModel:
public class TeamPlayersViewModel
{
public int Id {get;set;}
public string Name {get;set;}
public int Nid {get;set;}
public int PlayerIdId {get;set;}
public string PlayerName {get;set;}
...................................
...................................
}
现在,您的linq查询:
var z = (from y in db.Teamplayers
join p in db.Players on y.playerid equals p.Nid
select new TeamPlayersViewModel{
Id= y.Id,
PlayerName = p.PlayerName,
..........................
.......................... }).ToList<TeamPlayersViewModel>();
var z=(从db.Teamplayers中的y开始)
在db中加入p。玩家在y.playerid上等于p.Nid
选择新的TeamPlayerViewModel{
Id=y.Id,
PlayerName=p.PlayerName,
..........................
ToList();
现在在您的视图中更改模型:
@model List<MvcFFL.ViewModels.TeamPlayersViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@型号列表
@{
ViewBag.Title=“Index”;
}
指数
由于视图仅绑定到一个模型类,因此无法将两个模型对象传递给视图,因此需要创建一个视图模型类,该类将具有两个类的属性
假设您的模型如下所示:
public class TeamPlayers
{
public int Id {get;set;}
public int Name {get;set;}
public int Nid {get;set;}
..........................
..........................
}
public class Player
{
public int PlayerId {get;set;}
public int PlayerName {get;set;}
................................
................................
}
在项目中创建一个名为ViewModels的文件夹,并创建一个具有两个模型属性的ViewModel:
public class TeamPlayersViewModel
{
public int Id {get;set;}
public string Name {get;set;}
public int Nid {get;set;}
public int PlayerIdId {get;set;}
public string PlayerName {get;set;}
...................................
...................................
}
现在,您的linq查询:
var z = (from y in db.Teamplayers
join p in db.Players on y.playerid equals p.Nid
select new TeamPlayersViewModel{
Id= y.Id,
PlayerName = p.PlayerName,
..........................
.......................... }).ToList<TeamPlayersViewModel>();
var z=(从db.Teamplayers中的y开始)
在db中加入p。玩家在y.playerid上等于p.Nid
选择新的TeamPlayerViewModel{
Id=y.Id,
PlayerName=p.PlayerName,
..........................
ToList();
现在在您的视图中更改模型:
@model List<MvcFFL.ViewModels.TeamPlayersViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@型号列表
@{
ViewBag.Title=“Index”;
}
指数
由于视图仅绑定到一个模型类,因此无法将两个模型对象传递给视图,因此需要创建一个视图模型类,该类将具有两个类的属性
假设您的模型如下所示:
public class TeamPlayers
{
public int Id {get;set;}
public int Name {get;set;}
public int Nid {get;set;}
..........................
..........................
}
public class Player
{
public int PlayerId {get;set;}
public int PlayerName {get;set;}
................................
................................
}
在项目中创建一个名为ViewModels的文件夹,并创建一个具有两个模型属性的ViewModel:
public class TeamPlayersViewModel
{
public int Id {get;set;}
public string Name {get;set;}
public int Nid {get;set;}
public int PlayerIdId {get;set;}
public string PlayerName {get;set;}
...................................
...................................
}
现在,您的linq查询:
var z = (from y in db.Teamplayers
join p in db.Players on y.playerid equals p.Nid
select new TeamPlayersViewModel{
Id= y.Id,
PlayerName = p.PlayerName,
..........................
.......................... }).ToList<TeamPlayersViewModel>();
var z=(从db.Teamplayers中的y开始)
在db中加入p。玩家在y.playerid上等于p.Nid
选择新的TeamPlayerViewModel{
Id=y.Id,
PlayerName=p.PlayerName,
..........................
ToList();
现在在您的视图中更改模型:
@model List<MvcFFL.ViewModels.TeamPlayersViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@型号列表
@{
ViewBag.Title=“Index”;
}
指数
由于视图仅绑定到一个模型类,因此无法将两个模型对象传递给视图,因此需要创建一个视图模型类,该类将具有两个类的属性
假设您的模型如下所示:
public class TeamPlayers
{
public int Id {get;set;}
public int Name {get;set;}
public int Nid {get;set;}
..........................
..........................
}
public class Player
{
public int PlayerId {get;set;}
public int PlayerName {get;set;}
................................
................................
}
在项目中创建一个名为ViewModels的文件夹,并创建一个具有两个模型属性的ViewModel:
public class TeamPlayersViewModel
{
public int Id {get;set;}
public string Name {get;set;}
public int Nid {get;set;}
public int PlayerIdId {get;set;}
public string PlayerName {get;set;}
...................................
...................................
}
现在,您的linq查询:
var z = (from y in db.Teamplayers
join p in db.Players on y.playerid equals p.Nid
select new TeamPlayersViewModel{
Id= y.Id,
PlayerName = p.PlayerName,
..........................
.......................... }).ToList<TeamPlayersViewModel>();
var z=(从db.Teamplayers中的y开始)
在db中加入p。玩家在y.playerid上等于p.Nid
选择新的TeamPlayerViewModel{
Id=y.Id,
PlayerName=p.PlayerName,
..........................
ToList();
现在在您的视图中更改模型:
@model List<MvcFFL.ViewModels.TeamPlayersViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@型号列表
@{
ViewBag.Title=“Index”;
}
指数
首先创建一个新模型
class MyModel{
Teamplayers team{get; set;}
Players players{get; set;}
}
在视图中将模型类型设置为MyModel
然后
首先,创建一个新模型
class MyModel{
Teamplayers team{get; set;}
Players players{get; set;}
}
在视图中将模型类型设置为MyModel
然后
首先,创建一个新模型
class MyModel{
Teamplayers team{get; set;}
Players players{get; set;}
}
在视图中将模型类型设置为MyModel
然后
首先,创建一个新模型
class MyModel{
Teamplayers team{get; set;}
Players players{get; set;}
}
在视图中将模型类型设置为MyModel
然后
你必须创建一个视图模型,我认为你需要用IEnumerable替换IEnumerable而不是@model IEnumerable。我建议您只创建一个视图模型您必须创建一个视图模型我认为您需要用IEnumerable替换IEnumerable而不是@model IEnumerable。我建议您只创建一个视图模型您必须创建一个视图模型我认为您需要用IEnumerable替换IEnumerable而不是@model IEnumerable。我建议您只创建一个视图模型您必须创建一个视图模型我认为您需要用IEnumerable替换IEnumerable而不是@model IEnumerable。我建议你只创建一个视图模型当我第一次需要传递两个模型以供查看时,我也太迟钝了,但我们通过时间学习很高兴它有帮助…:)当我第一次需要传递两个模型以供查看时,我也太迟钝了,但我们通过时间学习很高兴它有帮助…:)当我第一次需要传递两个模型以供查看时,我也太迟钝了但我们学习的时间很高兴它