简体   繁体   English

使用模型和视图(MVC)

[英]Use of Model and View (MVC)

I have an application using c# and MVC5, with RazorEngine. 我有一个使用RazorEngine和c#和MVC5的应用程序。

My application manages requests (orders from clients) and I need to show them in tables. 我的应用程序管理请求(来自客户的订单),我需要在表中显示它们。 To achieve this I have a Controller (HistoryController), a View (Index) and a Model (MaterialRequestModel) which takes several parameters in the constructor: 为此,我有一个控制器(HistoryController),一个视图(Index)和一个模型(MaterialRequestModel),它们在构造函数中采用了几个参数:

 public MaterialRequestModel(MaterialRequest order, Employee aConcernedEmployee, Employee anOrderedbyEmployee, Office anOffice)

In my controller, HistoryController I have the following index method, which gets all requests completed or canceled: 在我的控制器HistoryController我具有以下索引方法,该方法将完成或取消所有请求:

public ActionResult Index()
{
    IQueryable<MaterialRequest> query = DB.MaterialRequest
                .Where(m => m.MaterialStatusId == MatStatus.A9Cancelled || m.MaterialStatusId == MatStatus.A8Complete);

    List<MaterialRequestModel> model= new List<MaterialRequestModel>();

    foreach (MaterialRequest req in query)
    {
        model.Add(new MaterialRequestModel(req, DB.Employees.Find(req.ConcernedEmployeeId), DB.Employees.Find(req.OrderedByEmployeeId), DB.Offices.Find(req.OfficeId)));
    }

    return View(model);
}

Now, I could simply pass the query to the the View, but that is not a good practice and the community (this one!) strongly suggests I use Models to avoid placing logic into the view. 现在,我可以简单地将query传递给View,但这不是一个好习惯,社区(这个!)强烈建议我使用Models以避免将逻辑放入视图中。

However, I can't help to think that the way I am building my model sucks terribly bad, because I am iterating over a large set of results. 但是,我不禁会以为我建立model的方式非常糟糕,因为我要遍历大量结果。

How can I improve this code and make it decent without the loop? 我如何才能改善此代码并使之不出现循环而又不错呢?

Additionally, should I pass everything as a parameter to my model, or should I just pass the DB object into the Models constructor and have it do the queries there? 另外,我应该将所有内容都作为参数传递给我的模型,还是应该仅将DB对象传递给Models构造函数并让它在那里进行查询?

        IQueryable<MaterialRequest> query= DB.MaterialRequest
            .Where(m => m.MaterialStatusId == MatStatus.A9Cancelled || m.MaterialStatusId == MatStatus.A8Complete)
            .Select(m => new MaterialRequestModel(m, DB.Employees.Find(m.ConcernedEmployeeId), DB.Employees.Find(m.OrderedByEmployeeId), DB.Offices.Find(m.OfficeId))
            .ToList();

If you just don't want to see the loop, you can use a select statement. 如果您只是不想看到循环,可以使用select语句。 and if this is EF, you have the added benefit of not querying your DB in the loop, since it will translate that select statement into sql. 如果是EF,那么您还有一个额外的好处,就是无需在循环中查询数据库,因为它将把select语句转换为sql。

Also, if you want to go to the next level, you can use foreign key references instead of all the DB.Employees.Find(...) Here's the result of my googling for that 另外,如果您想进入下一个级别,则可以使用外键引用而不是所有DB.Employees.Find(...) 这是我对此进行谷歌搜索的结果

I try to answer some of your questions :) 我尝试回答您的一些问题:)

eliminating the loop: 消除循环:

Create a query like Sam I am suggested. 建议创建类似Sam的查询。 This way you would get all the data with single query and you could eliminate the loop. 这样,您可以通过单个查询获取所有数据,并且可以消除循环。

the model: 该模型:

Personally I like POCOs a lot, because I looks cleaner to me. 我个人非常喜欢POCO ,因为我看上去更干净。 Thats why I would not pass the DB into my model. 这就是为什么我不将数据库传递到我的模型中的原因。

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

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