Shuould我们直接在ASP净MVC视图执行LINQ [英] Shuould we perform LINQ directly in asp net mvc views

查看:109
本文介绍了Shuould我们直接在ASP净MVC视图执行LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过我们对(ASP MVC 3.0)工作的项目看,我已经看到了code,这部分在我的ASPX视图之一:

Looking through the project we are working on (ASP MVC 3.0), I have seen this part of code in one of my ASPX views:

var groups = Model.GroupBy(t => new { t.OrganizationUnitName, t.OrganizationUnitId,   t.ServiceTermDate }).OrderBy(m =>m.Key.ServiceTermDate).ThenBy(m => m.Key. OrganizationUnitId);
  foreach (var group in groups){
             var data = group.Select(t => new 
                                   {
                                       t.PersonFullName, 
                                       t.ServiceTermStatusName,
                                       t.VisitTypeName,
                                       SubType = ControllerHelper.LocalizedPersonSubType(t.PersonSubTypeName),
                                       t.MedicalServiceName,
                                       t.PersonId,
                                       t.ServiceTermId,
                                       t.Phone,
                                       CountryName = t.Name,
                                       PersonUniqueNumber = t.GetUniqueIdentifyingNumber(),

                                   }).OrderBy(m => m.HoursFromMinutesFrom);

foreach(var item in data){%>
...............
//render table and table rows, for sample
    <tr>
        <td><%= item.PersonFullName%></td>
    </tr>
..............  
<%}%>
<%}%>               




<%}%>

我不知道这是最好的编码实践,不应LINQ语句放在控制器助手(或其他地方),而不是在直接看法?如果我是对的,怎么会利用最佳的编码方法来实现?

I am not sure this is best coding practice, shouldn't LINQ statement be placed in controller helper (or somewhere else) instead in view directly? And if I'm right, how that could be done utilizing best coding practices?

感谢您提前

看来,这是在视图中直接执行LINQ不仅是在错误的地方,但也是它提出了另一个有趣的问题:如果我们把它放到服务层或控制器或控制器助手,那么它将如何在这个传递案例 - 匿名类型IGrouping强类型视图

It seems that LINQ which is performed directly in the view is not only at the wrong place but also it raise another interesting question: if we place it into service layer or controller or controller helper, then how it would be passed in this case - anonymous type IGrouping to strongly typed view?

推荐答案

就个人而言,我不会在视图中使用LINQ。在控制器单元测试的目的,做到这一点。

Personally, I wouldn't use LINQ in the view. Do this in the controller for unit testing purposes.

如果有正在执行的逻辑,在一个更​​大的应用我的连招出来一个服务组件,该组件将包含所有的LINQ查询。

If there is logic being performed, in a larger application I'd even move it out to a services assembly which would contain all of your LINQ queries.

您视图应尽可能碱性。任何排序,分组或排序应该在你的控制器(带一个辅助方法,它是可以重复使用跨应用不同的动作的帮助preferably)来完成。

Your view should be as basic as possible. Any ordering, grouping or sorting should be done in your controller (preferably with the help of a helper method which is re-usable for different actions across the application).

这篇关于Shuould我们直接在ASP净MVC视图执行LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆