C# NET MVC:如何在视图中显示从一个相关模型到多个相关模型的详细信息

C# NET MVC:如何在视图中显示从一个相关模型到多个相关模型的详细信息,c#,asp.net-mvc,C#,Asp.net Mvc,所以基本上我有一个Post模型,它与创建它的用户相关联: public class Post { public int Id { get; set; } [Required] public string PostContent { get; set; } [Required] public DateTime DatePosted { get; set; } [Required] public string PostTitle { get;

所以基本上我有一个Post模型,它与创建它的用户相关联:

public class Post
{
    public int Id { get; set; }
    [Required]
    public string PostContent { get; set; }
    [Required]
    public DateTime DatePosted { get; set; }
    [Required]
    public string PostTitle { get; set; }

    public ApplicationUser ApplicationUser { get; set; }
}
应用程序用户有以下帖子:

public virtual ICollection<Post> Posts { get; set; }

最好/最简单的方法是什么?

您的Posts类/视图模型如下所示:

    public class PostViewModel
    {
        public int Id { get; set; }
        [Required]
        public string PostContent { get; set; }
        [Required]
        public DateTime DatePosted { get; set; }
        [Required]
        public string PostTitle { get; set; }

        public ApplicationUserViewModel PostOwner { get; set; }

        public List<ApplicationUserViewModel> PostContributors { get; set; }

    }
公共类PostViewModel
{
公共int Id{get;set;}
[必需]
公共字符串后内容{get;set;}
[必需]
public DateTime DatePosted{get;set;}
[必需]
公共字符串PostTitle{get;set;}
公共应用程序服务模型PostOwner{get;set;}
公共列表后置构造函数{get;set;}
}
您的ApplicationService模型可以如下所示:

    public class ApplicationUserViewModel
    {
        public int UserId { get; set; }
        [Required]
        public string UserName { get; set; }
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        [Required]
        public string Email { get; set; }

        public virtual List<dtoPost> Posts { get; set; }

    }
公共类应用程序服务模型
{
public int UserId{get;set;}
[必需]
公共字符串用户名{get;set;}
[必需]
公共字符串名{get;set;}
[必需]
公共字符串LastName{get;set;}
[必需]
公共字符串电子邮件{get;set;}
公共虚拟列表发布{get;set;}
}
添加dto以避免对视图模型的循环引用:

    public class dtoPost
    {
        public int Id { get; set; }
        [Required]
        public string PostContent { get; set; }
        [Required]
        public DateTime DatePosted { get; set; }
        [Required]
        public string PostTitle { get; set; }

        public ApplicationUserViewModel PostOwner { get; set; }

        public List<ApplicationUserViewModel> PostContributors { get; set; }

    }
公共类dtoPost
{
公共int Id{get;set;}
[必需]
公共字符串后内容{get;set;}
[必需]
public DateTime DatePosted{get;set;}
[必需]
公共字符串PostTitle{get;set;}
公共应用程序服务模型PostOwner{get;set;}
公共列表后置构造函数{get;set;}
}
您可以在Post的索引视图中访问Post所有者的电子邮件,并参考PostViewModel:

@model WordClues.ViewModels.PostViewModel

    <div>
        @Html.DisplayFor(m => m.PostOwner.Email)
    </div>
@model WordClues.ViewModels.PostViewModel
@DisplayFor(m=>m.PostOwner.Email)

您必须在代码中编写适当的控制器方法。顺便说一句,这是asp.net MVC。

不幸的是,我很确定没有人读这篇文章是心灵感应,而且“以下内容不起作用”也没什么可谈的。为什么不起作用?它不编译吗?您是否在运行时收到错误/异常?如果是,错误是什么?对不起。我猜我只是假设至少会有一些人有心灵感应;-)。不管是谁投票否决了我,请告诉我为什么,我会努力改进邮递服务。我是这样想的,但这更有意义;因此,我假设我必须构建一个PostViewModels列表,并在PostsController?@dhouse42的索引方法中返回该列表,我编辑了我的原始帖子。如果将应用程序设计为写入DB,那么dto将是更好的选择,而不是返回视图模型的引用。我意识到你的问题范围太广了。然而,它有助于审查您的设计,然后将这些视图模型放置到位,从而实现您的目标。另外,我还假设当你说它不起作用时,它们没有被显示出来。@dhouse42由于最大长度而添加另一篇文章:你的问题的答案取决于你在你的观点中试图实现什么?这是一个带有详细链接的帖子摘要吗?那么是的。你也可能想考虑你的观点是以文章为中心还是以用户为中心。如果你认为它回答了你的问题,请考虑一下:你回答了我的问题,谢谢!我投了更高的票,但直到我有了更高的声誉(15)@dhouse42谢谢!很高兴听到它有用!
@model WordClues.ViewModels.PostViewModel

    <div>
        @Html.DisplayFor(m => m.PostOwner.Email)
    </div>