C# foreach 或中继器 - 哪个更好?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/347472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
foreach or Repeater - which is better?
提问by Emma
I'm building a website in ASP.Net, using MVC, and need to list a set of results. Both of the following work as I want them to but I'm wondering which is faster, cleaner and/or better - or if another option entirely would be more appropriate?
我正在 ASP.Net 中建立一个网站,使用 MVC,并且需要列出一组结果。以下两种方法都按照我的意愿工作,但我想知道哪个更快、更清洁和/或更好 - 或者另一个选项是否更合适?
Note: ViewData.Model
is of type IEnumerable<Thing>
and I need to display more attributes than Name
- I've cropped the code for this example.
注意:ViewData.Model
是类型IEnumerable<Thing>
,我需要显示更多的属性Name
- 我已经裁剪了此示例的代码。
<% foreach (var thing in ViewData.Model)
{ %>
<p><%= thing.Name %></p>
<% }; %>
<% rptThings.DataSource = ViewData.Model;
rptThings.DataBind(); %>
<asp:Repeater ID="rptThings" runat="server">
<ItemTemplate>
<p><%# DataBinder.Eval(Container.DataItem, "Name") %></p>
</ItemTemplate>
</asp:Repeater>
采纳答案by Mehrdad Afshari
foreach
is definitely faster, if you don't specifically screw up something. Repeater
is cleaner of course, and more neatly separates UI and logic. Sometimes you need more conditions (other than different look even and odd rows) to render your stuff properly which makes foreach
the only choice.
foreach
如果你没有特别搞砸的话,肯定会更快。Repeater
当然更干净,并且更巧妙地将 UI 和逻辑分开。有时你需要更多的条件(除了不同的偶数行和奇数行)来正确渲染你的东西,这是foreach
唯一的选择。
I personally prefer Repeater
for normal situations and foreach
for more complex ones.
我个人更喜欢Repeater
正常情况和foreach
更复杂的情况。
EDIT: I was talking about plain ASP.NET with WebControls. For MVC and even pages that are mostly generated by code, I agree that foreach is more straightforward and cleaner.
编辑:我在谈论带有 WebControls 的普通 ASP.NET。对于 MVC 甚至大部分由代码生成的页面,我同意 foreach 更直接和更清晰。
回答by Pure.Krome
foreach
is the way to go for ASP.NET MVC. Why? i personally avoid anylegacy asp:xxx
controls .. because they could possibly have the bloat that exists with the webforms model. Secondly, what about all the event delegates
you have to wire up? you're starting to mix and match architectures, IMO, so this could seriously lead to real spagetti code with crazy maintenence and support issues. (IMO: DataBinder.Eval == very evil :( :( :( )
foreach
是 ASP.NET MVC 的方法。为什么?我个人避免使用任何遗留asp:xxx
控件.. 因为它们可能具有 webforms 模型中存在的膨胀。其次,event delegates
你必须连接的所有东西呢?你开始混合和匹配架构,IMO,所以这可能会严重导致真正的意大利面条代码,具有疯狂的维护和支持问题。(国际海事组织:DataBinder.Eval == 非常邪恶 :( :( :( )
The only asp:xxx
control i use is the mastpage / content control
(because there are no alternatives to it).
asp:xxx
我使用的唯一控件是mastpage / content control
(因为没有其他选择)。
Lastly, doing foreach
in asp.net mvc is NOT spagetti code, as many people believe. I know i did when i first saw the initial mvc demo's. If anything, it actually makes the UI so much more cleaner than before, imo .. so much more maintainable. IMO, spagetti code is when u have lots of <% .. %>
doing business logic and ui logic and (gag) db access. Remember, that's what peeps did in the wild west of asp.net classic:P
最后,foreach
正如许多人认为的那样,在 asp.net mvc 中做的不是意大利面条代码。当我第一次看到最初的 mvc 演示时,我知道我做了。如果有的话,它实际上使 UI 比以前更干净,imo .. 更易于维护。IMO,意大利面代码是当你有很多<% .. %>
业务逻辑和用户界面逻辑以及(gag)数据库访问时。请记住,这就是 peeps 在 asp.net classic的狂野西部所做的:P
Summary
概括
Stick with foreach
and avoid using any webform controls - it's simple, very efficient and very possible to do.
坚持foreach
并避免使用任何 webform 控件 - 这很简单,非常有效并且很有可能做到。
回答by Craig
I use the extension method repeater from Phil Haack. Best of both worlds. http://haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx
我使用 Phil Haack 的扩展方法中继器。两全其美。http://haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx
回答by Tim Scott
Here's another option. I have not used this myself, but looks interesting.
这是另一种选择。我自己没有使用过这个,但看起来很有趣。