简体   繁体   English

在ASP.NET MVC中,View中的IF..ELSE语句是否被否决?

[英]Are IF..ELSE statements in a View frowned upon in ASP.NET MVC?

I know that you want to keep logic out of your views. 我知道您想让逻辑脱离您的观点。 I can elimate most loops by using DisplayFor/EditorFor and passing IEnumerables to the view. 通过使用DisplayFor / EditorFor并将IEnumerables传递给视图,我可以消除大多数循环。

What about IF statements? IF语句呢? should they be avoided completely in views? 是否应该在视图中完全避免它们? used sparingly? 谨慎使用? as a last resort? 作为最后的手段?

Say you wanted to show hide an element based on a User role...How would u go about doing this without an IF statement...a completely seperate view perhaps? 假设您想根据用户角色显示隐藏元素...您如何在没有IF语句的情况下执行此操作...也许是一个完全独立的视图?

Just trying to get an idea of best practices. 只是想了解最佳做法。

Thanks! 谢谢!

Be consistent, and keep in mind the purpose of the view - to produce your HTML. 保持一致,并牢记视图的目的-生成HTML。 Toward that end, certainly you will need some if constructs here or there. 为此,在这里或那里肯定会需要一些if构造。 I think some people are suggesting you stick to some pie-in-the-sky, ultra-nitpicky purism here at the expense of usable, functional, well-defined code. 我认为有些人建议您在此处坚持使用一些空洞的,过于挑剔的纯粹主义,以牺牲可用的,功能强大的,定义明确的代码为代价。

只要您不最终将后端逻辑放入视图中,就可以在视图中使用if

Rob Conery has a rule of thumb that states "if there's and IF, make a helper" . 罗伯·科纳里(Rob Conery)有一条经验法则,规定“如果有,如果有,请帮忙” Personally, I would say "use sparingly". 就个人而言,我会说“谨慎使用”。 I avoid it as much as possible because it makes things more difficult to unit test. 我尽可能避免使用它,因为这会使单元测试变得更加困难。

For the situation where you want to hide elements based on user roles: For simple scenarios, I would probably put the check directly in the view. 对于要根据用户角色隐藏元素的情况:对于简单的情况,我可能会将检查直接放在视图中。 Usually I try to still make these more terse and testable, though. 通常,尽管如此,我通常还是尝试使它们更加简洁和可测试。 So instead of: 所以代替:

@if (HttpContext.Current.User.IsInRole("admin")
{
    // Show admin stuff
}

I would do something like: 我会做类似的事情:

@if (Model.UserIsAdmin)
{
    // Show admin stuff
}

On the other hand, if these kinds of checks started getting speckled all over your views, I'd probably create the elements conditionally in the viewmodel first, and then just display what's been built. 另一方面,如果这些检查开始遍布您的所有视图,那么我可能会先在viewmodel中有条件地创建元素,然后仅显示所构建的内容。 Hope that helps. 希望能有所帮助。

Basically every View should display whats passed in ViewModel. 基本上每个视图都应显示ViewModel中传递的内容。 If ViewModel is not enough, then I would look for a way of improving the ViewModel creation itself, not the View's logic. 如果ViewModel还不够,那么我会寻找一种改进ViewModel创建本身的方法,而不是View的逻辑。

All conditionals CAN be evaluated upon ViewModel creation. 创建ViewModel时可以评估所有条件。

Of course, It all depends on how much custom-logic in View does Your project organization tolerates. 当然,这完全取决于您的项目组织可以在View中容纳多少自定义逻辑。

If / else can be used sparingly, in my opinion, but for the example you mention, hide an element based on role - the if check should definitely not be in the view. 我认为,如果/ else可以少量使用,但对于您提到的示例,请隐藏基于角色的元素-if检查绝对不应出现在视图中。 Write extensions and helpers where needed. 在需要的地方编写扩展程序和帮助程序。

我认为最好避免在视图中使用,在示例中应避免在视图中使用business(Model)或application(Controller)逻辑,可以创建不同的Partial View以显示某些依赖于用户角色的视图,并在Controller中放置您需要显示的视图的逻辑

我建议在视图中通过“ if”隐藏元素,但是在代码中必须禁用由隐藏元素激活的功能(方法)。

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

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