asp.net-mvc MVC 剃刀 @foreach
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11261590/
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
MVC Razor @foreach
提问by Nate Pet
I heard that having @foreach inside of a view is a no-no. Meaning, the view should not have any logic in it. What is the best practice on where the logic for the @foreach should be at?
我听说在视图中使用 @foreach 是禁忌。意思是,视图中不应该有任何逻辑。@foreach 的逻辑应该在哪里的最佳实践是什么?
@foreach..
回答by Darin Dimitrov
What is the best practice on where the logic for the @foreach should be at?
@foreach 的逻辑应该在哪里的最佳实践是什么?
Nowhere, just get rid of it. You could use editor or display templates.
无处可去,干掉它。您可以使用编辑器或显示模板。
So for example:
例如:
@foreach (var item in Model.Foos)
{
<div>@item.Bar</div>
}
could perfectly fine be replaced by a display template:
完全可以用显示模板替换:
@Html.DisplayFor(x => x.Foos)
and then you will define the corresponding display template (if you don't like the default one). So you would define a reusable template ~/Views/Shared/DisplayTemplates/Foo.cshtmlwhich will automatically be rendered by the framework for each element of the Foos collection (IEnumerable<Foo> Foos { get; set; }):
然后你将定义相应的显示模板(如果你不喜欢默认的)。因此,您将定义一个可重用模板~/Views/Shared/DisplayTemplates/Foo.cshtml,该模板将由框架为 Foos 集合 ( IEnumerable<Foo> Foos { get; set; }) 的每个元素自动呈现:
@model Foo
<div>@Model.Bar</div>
Obviously exactly the same conventions apply for editor templates which should be used in case you want to show some input fields allowing you to edit the view model in contrast to just displaying it as readonly.
显然,完全相同的约定适用于编辑器模板,如果您想显示一些输入字段,允许您编辑视图模型,而不是仅将其显示为只读,则应使用这些约定。
回答by JonoW
When people say don't put logic in views, they're usually referring to business logic, not rendering logic. In my humble opinion, I think using @foreach in views is perfectly fine.
当人们说不要在视图中放置逻辑时,他们通常指的是业务逻辑,而不是渲染逻辑。以我的拙见,我认为在视图中使用 @foreach 非常好。
回答by Mihai Labo
I'm using @foreachwhen I send an entity that contains a list of entities ( for example to display 2 grids in 1 view )
我在@foreach发送包含实体列表的实体时使用(例如在 1 个视图中显示 2 个网格)
For example if I'm sending as model the entity Foo that contains Foo1(List<Foo1>)and Foo2(List<Foo2>)
例如,如果我将包含Foo1(List<Foo1>)和的实体 Foo 作为模型发送Foo2(List<Foo2>)
I can refer to the first List with:
我可以参考第一个列表:
@foreach (var item in Model.Foo.Foo1)
{
@Html.DisplayFor(modelItem=> item.fooName)
}
回答by Nicholas King
a reply to @DarinDimitrov for a case where i have used foreach in a razor view.
对@DarinDimitrov 的回复,用于我在剃刀视图中使用 foreach 的情况。
<li><label for="category">Category</label>
<select id="category">
<option value="0">All</option>
@foreach(Category c in Model.Categories)
{
<option title="@c.Description" value="@c.CategoryID">@c.Name</option>
}
</select>
</li>
回答by Tiberiu Craciun
The answerwill not work when using the overload to indicate the template @Html.DisplayFor(x => x.Foos, "YourTemplateName).
当使用重载来指示模板时,答案将不起作用@Html.DisplayFor(x => x.Foos, "YourTemplateName)。
Seems to be designed that way, see this case. Also the exception the framework gives (about the type not been as expected) is quite misleading and fooled me on the first try (thanks @CodeCaster)
似乎是这样设计的,见这个案例。此外,框架给出的异常(关于类型不符合预期)在第一次尝试时非常具有误导性并欺骗了我(感谢@CodeCaster)
In this case you have touse @foreach
在这种情况下,您必须使用@foreach
@foreach (var item in Model.Foos)
{
@Html.DisplayFor(x => item, "FooTemplate")
}

