多少逻辑在ASP.NET MVC的意见是允许的? [英] How much logic is allowed in ASP.NET MVC views?

查看:156
本文介绍了多少逻辑在ASP.NET MVC的意见是允许的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在看着ASP.NET MVC网站的样本,我看到相当多的与意见嵌入式逻辑,例如例子:

In looking at samples of ASP.NET MVC sites, I'm seeing quite a bit of examples with embedded logic in the views, e.g.:

<% if (customerIsAllowed)
   { %>

   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>

<% }  else {%>

   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>

<% } %>

虽然这似乎是我错了,因为它是因为其余的那种,我们试图从ASP 3.0跑不掉,我甚至听到了一些播客如何逻辑的观点一点点就可以了的事MVC框架正在我们没有在ASP 3.0有结构的照顾。

Although this seems wrong to me since it is the kind of thing we were trying to get away from in ASP 3.0, I have even heard in some podcasts how "a little bit of logic in view is ok" since the rest of the MVC framework is taking care of the structure that we didn't have in ASP 3.0.

是否有任何MVC约定,指定什么样的,有多少逻辑的观点是允许的?

Are there any MVC conventions specifying what kind and how much logic is allowed in views?

推荐答案

这取决于对逻辑的理由。如果逻辑是基于一些属性由控制器传递给它选择一个备用presentation,它可能是好的。这可以让你有些人认为的重用。而不必重新创建(重复)为每个自定义权限的完整视图,可以在一些数据,使视图基于此特权进行定制通过。

It depends on the reason for the logic. If the logic is choosing an alternate presentation based on some property passed to it by the controller, it is probably ok. This allows you some view reuse. Instead of having to recreate (and repeat) an entire view for each custom privilege, you can pass in some data that allows the view to be customized based on this privilege.

我认为这是一个理想的MVC和干严格执行(不要重复自己)之间的务实平衡。在某些情况下,它是明智违反一个或另一个,如果你不能轻易达到两者。在清楚模型和基本观点是一样的,放一点逻辑视图,让您的看法是干的合理情况。

I think of this as a pragmatic balance between an idealized MVC and strict enforcement of DRY (don't repeat yourself). In some situations it is wiser to violate one or the other if you can't attain both easily. In the case where clearly the model and the basic view is the same, putting a little logic in the view to keep your views DRY is reasonable.

这篇关于多少逻辑在ASP.NET MVC的意见是允许的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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