简体   繁体   English

如何在视图中使用来自多个类(表)的信息?

[英]How can I use information from multiple classes (tables) in a view?

In my database, UserID is a foreign key that goes to the UserId column in my aspnet_Users table. 在我的数据库中,UserID是一个外键,它转到我的aspnet_Users表中的UserId列。 Consider post to be like a post in a forum, a blog entry, whatever... When I list the posts in my view I want to display the username of the person instead of the guid. 将帖子视为论坛中的帖子,博客条目,等等...当我在视图中列出帖子时,我想显示该人的用户名而不是guid。 How can I retrieve this and send it to my view at the same time? 如何检索并同时将其发送到我的视图?

namespace MySite.Models
{
    public class Post
    {
        public Guid PostID { get; set; }
        public Guid UserID { get; set; }
        public DateTime PostedDate { get; set; }
        public string PostContent { get; set; }
    }
    public class DBContexts : DbContext
    {
        public DbSet<Post> Posts { get; set; }
    }
}
public class PostController : Controller
{
    DBContexts db = new DBContexts();

    public ActionResult Index()
    {
        var posts = db.Posts.ToList();
        return View(posts);
    }
}

One alternative would be to put the aspnet_Users table into your Entity Model, and then do a join to retrieve the additional user information when you request the posts. 一种替代方法是将aspnet_Users表放入您的实体模型,然后在请求帖子时进行连接以检索其他用户信息。

var posts = (from p in db.Posts
             join u in db.AspNet_Users on p.UserId equals u.UserId
             select new { Post = p, Username = u.UserName }).ToList();

I believe that you could select the entire entity p here into an anonymous type, rather than having to select each column. 我相信你可以在这里选择整个实体p为匿名类型,而不是必须选择每一列。 If not in this syntax, a slightly different one such as using groupby would do it. 如果不是这种语法,稍微不同的一个,比如使用groupby就可以了。

Incidently I would advise you recreate the ObjectContext for each request, and also consider using a repository pattern so you can avoid repeating more complex calls like this, and keep them in a single place. 我会建议你为每个请求重新创建ObjectContext,并考虑使用存储库模式,这样你就可以避免重复这样复杂的调用,并将它们保存在一个地方。 (It's also great for unit testing). (它对单元测试也很有用)。

Alternatively, if you don't want to include the aspnet_Users table in your entity model, you could create a view in the database, and then add it as a View to the entity model. 或者,如果您不想在实体模型中包含aspnet_Users表,则可以在数据库中创建一个视图,然后将其作为View添加到实体模型中。 Perhaps even instead of the normal Posts table, but you wouldn't be able to insert entities to the view, and I assume this is functionality you will want. 也许甚至不是普通的Posts表,但你不能将实体插入到视图中,我认为这是你想要的功能。

EDIT (Using in the view): 编辑(在视图中使用):

The variable posts is an anonymous type. 变量帖子是匿名类型。 The C# compiler is able to let you use this in a strongly typed way within the method it is defined, but once you leave that scope, it is only known as an object. C#编译器能够让你在它定义的方法中以强类型的方式使用它,但是一旦你离开那个范围,它就只被称为一个对象。 This is why all the properties are not visible in the view. 这就是为什么视图中看不到所有属性的原因。

My advice would be to define a ViewModel class, which contains this data through compile time properties. 我的建议是定义一个ViewModel类,它通过编译时属性包含这些数据。 For example: 例如:

public class PostViewModel
{
    public Post Post { get; set; }
    public string Username { get; set; }
}

Then you can modify the above query to be something more like: 然后你可以修改上面的查询更像是:

...select new PostViewModel { Post = p, Username = u.UserName }).ToList();

If you make your MVC view extend ViewPage<PostViewModel>, then you should now be able to easily access the Post and Username properties on your Model. 如果您使MVC视图扩展ViewPage <PostViewModel>,那么您现在应该能够轻松访问模型上的Post和Username属性。

You can always use the viewbag to send additional info to your view. 您始终可以使用viewbag向视图发送其他信息。

Relevant. 相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM