从MVC中的多个表中选择数据

时间:2021-04-12 15:41:44

I'm am in the middle of creating my first, substantial .NET MVC application. I have come across a problem and I am not quite sure the proper way to go about it.

我正在创建我的第一个。net MVC应用程序。我遇到了一个问题,我不太确定该怎么做。

In my application I have quite a large database. For a number of features I need to select data from up to 5 tables and send it back to the view, and I am not quite sure how to go about it since view takes either Models or View Models?

在我的应用程序中,我有一个相当大的数据库。对于许多特性,我需要从多达5个表中选择数据并将其发送回视图,我不太确定该如何进行,因为视图采用模型或视图模型?

I understand the concept of View Models quite well, but is creating one every time I need to send data from multiple tables the only solution to this? And if so could anyone tell me the best practices when doing it

我很理解视图模型的概念,但是每次需要从多个表中发送数据时创建一个视图模型是唯一的解决方案吗?如果是这样的话,谁能告诉我最好的做法

Thanks in advance for any help

谢谢你的帮助

1 个解决方案

#1


1  

Yep, you'll have to have a View Models per view. I work on application with about 600 views and we tried re-cycling view models and it ended up in tears. Now there is a model for each view (mostly).

是的,每个视图都必须有一个视图模型。我开发的应用程序有大约600个视图,我们尝试了重新循环的视图模型,结果以失败告终。现在每个视图都有一个模型(主要是)。

To send data from multiple tables you'll need to run joins on your tables and select into a view model.

要从多个表发送数据,需要在表上运行连接并选择到视图模型中。

Here I presume you use Entity Framework:

这里我假设你使用的是实体框架:

public class ComplexViewModel
{
    public String Name { get; set; }
    public String Category { get; set; }
    public String Level { get; set; }
}

var db = new MyDbContext();
var result = from name in db.Names
             join category in db.Categories on name.CategoryId equals category.CategoryId
             join level in db.Levels on category.LevelId equals level.LevelId
             select new ComplexViewModel()
                {
                    Name = name.Name,
                    Category = category.CategoryName,
                    Level = level.LevelName,
                };
return result.ToList();               

More examples of joins can be found are recommended to review.

可以找到更多的连接示例,建议您进行检查。

#1


1  

Yep, you'll have to have a View Models per view. I work on application with about 600 views and we tried re-cycling view models and it ended up in tears. Now there is a model for each view (mostly).

是的,每个视图都必须有一个视图模型。我开发的应用程序有大约600个视图,我们尝试了重新循环的视图模型,结果以失败告终。现在每个视图都有一个模型(主要是)。

To send data from multiple tables you'll need to run joins on your tables and select into a view model.

要从多个表发送数据,需要在表上运行连接并选择到视图模型中。

Here I presume you use Entity Framework:

这里我假设你使用的是实体框架:

public class ComplexViewModel
{
    public String Name { get; set; }
    public String Category { get; set; }
    public String Level { get; set; }
}

var db = new MyDbContext();
var result = from name in db.Names
             join category in db.Categories on name.CategoryId equals category.CategoryId
             join level in db.Levels on category.LevelId equals level.LevelId
             select new ComplexViewModel()
                {
                    Name = name.Name,
                    Category = category.CategoryName,
                    Level = level.LevelName,
                };
return result.ToList();               

More examples of joins can be found are recommended to review.

可以找到更多的连接示例,建议您进行检查。