【问题标题】:MVC, Entity Framework Select data from multiple modelsMVC,Entity Framework 从多个模型中选择数据
【发布时间】:2012-06-28 09:06:53
【问题描述】:

在 ASP.NET MVC3 Razor 项目中,我有 2 个模型

public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Contents { get; set; }
        public int Author { get; set; }
    }

 public class Author
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }

Post.Author 字段链接到Author.Id 字段

在一个视图中,我需要显示列表

Post.Title
Post.Contents
Author.Name

如何显示加入(来自)两个模型的信息?

注意:我想我需要使用ViewModel 并将视图与IEnumerable 列表绑定,但我不知道如何从两个模型中选择数据 em>

【问题讨论】:

    标签: asp.net asp.net-mvc-3 entity-framework razor


    【解决方案1】:

    您可以创建一个视图模型,该模型仅具有您希望在视图上显示的属性

    public class PostViewModel
    {
            public int Id { get; set; }
            public string Title { get; set; }
            public string Contents { get; set; }
            public string AuthorName { get; set; }
    
    }
    

    您在控制器操作中使用您的数据填充此视图模型,并采取必要的连接

    public ActionResult GetAuthorInfor()
    {
       var query = //context.Post join with context.Author
                   Select new  PostViewModel()
                   {
                      Id = post.id,
                      Title = post.title,
                      Contents = post.contents,
                      AuthorName = author.authorname
                   }
       return view(query.Single());
    }
    

    并创建一个类型化的视图来渲染这个模型。

    【讨论】:

    • 感谢您的回答...它的工作。 (必须将 query.Single() 更改为 query.ToList())
    【解决方案2】:

    模型Post.cs

    public class Post
        {
            public int Id { get; set; }
            public string Title { get; set; }
            public string Contents { get; set; }
            public int AuthorID { get; set; }
    
            public virtual Author Author { get; set; }
        }
    

    模型Author.cs

    public class Author
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Email { get; set; }
            public virtual ICollection<Post> Posts { get; set; }
        }
    

    DBContext 类:

    public class SampleDB : DbContext
        {
            public DbSet<Author> Authors{ get; set; }
            public DbSet<Post> Posts{ get; set; }
        }
    

    I.Way(使用直接视图)

    你可以像这样在 View 上使用:

     Samp.Models.SampleDB dbPosts = new Samp.Models.SampleDB();
     foreach (var post in dbPosts.Posts.ToList())
     {
       string post_Title = post.title;
       string post_Contents = post.Contents;
       string author_Name = post.Author.Name;
     }
    

    II.Way(通过控制器使用)-推荐-

    你可以像这样在控制器上使用:

    Samp.Models.SampleDB db = new Samp.Models.SampleDB();
    
     public ActionResult Index()
     {
       return View(db.Posts.ToList());
     }
    

    视图上使用它:

    @model IEnumerable<Samp.Models.Post>
    
    
    foreach (var post in Model.Posts.ToList())
         {
           string post_Title = post.title;
           string post_Contents = post.Contents;
           string author_Name = post.Author.Name;
         }
    

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 2014-05-02
      • 2023-04-02
      • 2012-10-20
      • 1970-01-01
      相关资源
      最近更新 更多