如何避免在视图中循环? [英] How can I avoid looping in a View?

查看:125
本文介绍了如何避免在视图中循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我建立我的看法,我想消灭尽可能多的处理逻辑成为可能。理想情况下,我想坚持只用HTML和渲染值与剃刀。

因此​​,假设我有BarModel对象列表的FooModel:

 公共类FooModel
{
    公开名单< BarModel>酒吧{搞定;组; }    公共字符串FoosFirstValue {搞定;组; }
    公共字符串FoosOtherValue {搞定;组; }
}公共类BarModel
{
    公共字符串someValue中获得{;组; }
    公共字符串SomeOtherValue {搞定;组; }
}

在我看来,我通过需要循环,并显示在我的酒吧元素:

  @model MyApp.FooModel
< D​​IV> @ Model.FoosFirstValue< / DIV>
< D​​IV> @ Model.FoosOtherValue< / DIV>
< D​​IV>
    酒吧:
    < UL>
        @foreach(在Model.Bars VAR条)
        {
            <立GT; @ bar.SomeValue:@ bar.SomeOtherValue< /李>
        }
    < / UL>
< / DIV>

反正是有,以避免在我看来,使用循环?


解决方案

而在一个MVC视图循环并不一定是坏事,一些较真(像你似乎是)preFER,以保持自己的观点超洁净并使任何处理逻辑的。

幸运的是,没有在MVC一个鲜为人知的宝石,它可以采取的对象的IEnumerable列表,并呈现给绑定到单一对象的编辑/显示模板 - 它可以为您处理循环

例如,如果你设置了显示模板局部视图,我们称之为BarModel.cshtml(应存放在之一PartialViewLocationFormats搜索路径,下DispalyTemplate子文件夹中):

  @model MyApp.BarModel
<立GT; @ Model.SomeValue:@ Model.SomeOtherValue< /李>

现在,在你看来,你可以简单地使用调出该显示模板 Html.DisplayFor ,并在完整列表传递作为模型:

 < D​​IV>
    酒吧:
    < UL>
        @ Html.DisplayFor(M = GT; m.Bars)
    < / UL>
< / DIV>

就是这样。这将需要的BarModels的列表,并生成用于列表中每个BarModel对象部分显示tempalte

当然,你简单的例子,它是真正到个人preference,因为这不是一个重大的好处,除非你只是只是想摆脱循环逻辑的。

不过,如果你需要在同一个视图中的多个位置分享这显示模板,或者在不同的意见,这是值得的建立你的看法是这样,并降低复杂性和code重复。

另外一个侧面说明:这种方法假设要在列表中显示的每个项目,并在订货它存储在列表中。如果您需要有条件地显示项目,还是希望以不同的顺序来显示,这种方法是行不通的。

不过,如果是这样的话,我会建议设立完全按照自己的需要在视图中显示的列表中。也就是说,做任何过滤/控制器/服务,建立在列表中的排序,并留下您的看法,只是渲染你的模型。

When I build my views, I'd like to eliminate as much processing logic as possible. Ideally, I'd like to stick to just HTML and rendering values with Razor.

So suppose I have a FooModel with a list of BarModel objects:

public class FooModel
{
    public List<BarModel> Bars { get; set; }

    public string FoosFirstValue { get; set; }
    public string FoosOtherValue { get; set; }
}

public class BarModel
{
    public string SomeValue { get; set; }
    public string SomeOtherValue { get; set; }
}

And in my view, I need to loop through and display elements on my Bars:

@model MyApp.FooModel
<div>@Model.FoosFirstValue</div>
<div>@Model.FoosOtherValue </div>
<div>
    Bars:
    <ul>
        @foreach (var bar in Model.Bars)
        {
            <li>@bar.SomeValue: @bar.SomeOtherValue</li>
        }
    </ul>
</div>

Is there anyway to avoid using a loop in my view?

解决方案

While looping in an MVC view isn't necessarily a bad thing, some purists (like you seem to be) prefer to keep their views super clean and void of any processing logic.

Fortunately for you, there is a little known jewel in MVC that it can take an IEnumerable list of objects, and render that to a editor/display template bound to the singular object -- it handles the looping for you.

For example, if you set up a display template partial view, let's call it BarModel.cshtml (should be stored in one of the PartialViewLocationFormats search paths, under a "DispalyTemplate" sub-folder):

@model MyApp.BarModel
<li>@Model.SomeValue: @Model.SomeOtherValue</li>

Now, in your view, you can simply call out to the display template using Html.DisplayFor, and pass in the full list as the model:

<div>
    Bars:
    <ul>
        @Html.DisplayFor(m => m.Bars)
    </ul>
</div>

And that's it. It will take your list of BarModels, and generate the partial display tempalte for each BarModel object in the list.

Of course, with your simple example, it's really down to personal preference, as there's not a major benefit, unless you simply just want to get rid of the looping logic.

But if you ever need to share this display template in multiple locations on the same view, or in different views, it really pays off to build your views like this and reduce complexity and code duplication.

One other side note: This approach assumes that you want to display every item in the list, and in the order it is stored in the list. If you need to conditionally display items, or want to display in a different order, this approach won't work.

However, if this is the case, I would suggest setting up the list exactly as you need it displayed in the view. That is, do any filtering/sorting within the controller/service that builds the list, and leave your view to just rendering you models.

这篇关于如何避免在视图中循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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