Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用自定义模型通过LINQ查看相关数据的详细信息_C#_Asp.net Mvc_Linq_Entity Framework 6 - Fatal编程技术网

C# 使用自定义模型通过LINQ查看相关数据的详细信息

C# 使用自定义模型通过LINQ查看相关数据的详细信息,c#,asp.net-mvc,linq,entity-framework-6,C#,Asp.net Mvc,Linq,Entity Framework 6,由于我是ASP的新手,因此我对如何在相关父项的详细信息页面上反映通过LINQ派生的模型/表的某些特定列的列表感到困惑。 考虑两个类的例子: public class Tournament { [Key] public string TournamentID { get; set; } public DateTime TournamentDate { get; set; } public string Place { get; set; } [Foreig

由于我是ASP的新手,因此我对如何在相关父项的详细信息页面上反映通过LINQ派生的模型/表的某些特定列的列表感到困惑。 考虑两个类的例子:

public class Tournament
{
    [Key]
    public string TournamentID { get; set; }
    public DateTime TournamentDate { get; set; }
    public string Place { get; set; }

    [ForeignKey("TeamA")]
    public string TeamAID { get; set; }
    public Team TeamA { get; set; }

    [ForeignKey("TeamB")]
    public string TeamBID { get; set; }
    public Team TeamB { get; set; }
}

public class Team
{
    [Key]
    public string TeamID { get; set; }
    public string TeamName { get; set; }
    public string Captain { get; set; }

    [InverseProperty("TeamA")]
    public ICollection<Tournament> TeamA { get; set; }

    [InverseProperty("TeamB")]
    public ICollection<Tournament> TeamB { get; set; }
}
以及控制器动作,如:

    public async Task<IActionResult> Index2(string teamId)
    {
        var gamesList = ((from x in _context.Tournaments
                                .Where(x => x.TeamAID == teamId)
                                select new GamesList
                                {
                                    TID = x.TournamentID,
                                    TDate = x.TournamentDate,
                                    TeamLinks = x.TeamAID,
                                    TeamNames = x.TeamB.TeamName,
                                    TPlace = x.Place
                                })
                        .Concat(from x in _context.Tournaments
                                .Where(x => x.TeamBID == teamId)
                                select new GamesList
                                {
                                    TID = x.TournamentID,
                                    TDate = x.TournamentDate,
                                    TeamLinks = x.TeamBID,
                                    TeamNames = x.TeamA.TeamName,
                                    TPlace = x.Place
                                })).OrderBy(x => x.TDate);

        return View(await gamesList.ToListAsync());
    }
因此,将相同的列表连接起来,但团队ID和名称会翻转,从而导致所有ID都编译在名为TeamLinks的属性中,而竞争对手的名称则排列在TeamNames中

现在,可以通过使用@model IEnumerable来表示所述列表,但是作为主要目标,指定了如何在团队的详细信息下表示这样的列表,即也拥有TeamID==TeamID的列表


谢谢。

您需要从表示视图中所需内容的视图模型开始

public class TeamVM
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string Captain { get; set; }
    public IEnumerable<TournamentVM> Tournaments { get; set; }
}
public class TournamentVM
{
    public string ID { get; set; }
    public DateTime Date { get; set; }
    public string Place { get; set; }
    public string Competitor { get; set; }
}
@model TeamVM
....
<div>@Model.ID</div>
....
<table>
   <thead> ... </thead>
   <tbody>
       @foreach(var item in Model.Tournaments)
       {
           <tr>
               <td>@item.ID</td>
               <td>@item.Date</td>
               ....
           </tr>
       }
    </tbody>
</table>
在我看来

public class TeamVM
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string Captain { get; set; }
    public IEnumerable<TournamentVM> Tournaments { get; set; }
}
public class TournamentVM
{
    public string ID { get; set; }
    public DateTime Date { get; set; }
    public string Place { get; set; }
    public string Competitor { get; set; }
}
@model TeamVM
....
<div>@Model.ID</div>
....
<table>
   <thead> ... </thead>
   <tbody>
       @foreach(var item in Model.Tournaments)
       {
           <tr>
               <td>@item.ID</td>
               <td>@item.Date</td>
               ....
           </tr>
       }
    </tbody>
</table>

阅读视图模型Hanks Stephen,使用相同的方法,我在操作结束时发现了一个错误,即..OrderByx=>x.Date;并删除了该文件;最后但是,我仍然收到一个错误,如:ArgumentNullException:Value不能为null。参数名称:源。原因是什么;只是一个打字错误现在被编辑了。我不知道您的ArgumentNullException-它与我显示的代码无关。你从哪里又得到了例外?伙计。但是,该异常会反映在突出显示控制器操作行的网页上:TeamVM model=new TeamVM。当然,我会展示屏幕截图,但我发现自己无法在此评论中插入图像。请尝试缩小范围。我假设您已经调试了Team=db.Teams。。。。确保您获得有效的数据?评出锦标赛=team.TeamA。。。。bit etc查看异常在哪里我刚刚注意到您的导航属性不是虚拟的,因此可能存在延迟加载问题。