简体   繁体   English

MVC3视图中的条件逻辑是一种好的做法吗?

[英]Is conditional logic in an MVC3 view a good practice?

Here's an example view, I use it to output every Category in my database. 这是一个示例视图,我用它来输出数据库中的每个Category It's a recursive relationship, so a Category can have a List<Category> of Subcategories. 它是递归关系,因此Category可以具有子类别的List<Category>

@model DSS.WebUI.Models.CategoriaModel

<div class="categories">
    <h3>
        @if (Model.Subcategorias.Count > 0)
        {
            <img src="http://i.imgur.com/t5UXT.gif" />   
            <a href="#">@Model.Nombre</a>
            <p class="subtext">@Model.Encabezado</p>
        }
        else
        {
            <a class="nochild" href="#">@Model.Nombre</a>
            <p class="subtext nochild">@Model.Encabezado</p>
        }
    </h3>
    <div>
        <ul>
            @Html.DisplayFor(x => x.Subcategorias)
        </ul>
    </div>    
</div>

Is conditional logic like this kosher? 条件逻辑是否像这样犹太洁食? Or is it a code smell I should avoid and how? 或者这是我应该避免的代码气味以及如何?

This kind of conditional logic looks fine to me. 这种条件逻辑对我来说很好。 Based on the number of sub-categories you have in your view model you are generating one or another html fragment. 根据视图模型中的子类别数,您将生成一个或另一个html片段。 What would have been bad is to repeat this exact same condition with the same output in many places of your application. 在您的应用程序的许多地方使用相同的输出重复这个完全相同的条件会有什么不好。 In this case you could externalize it into a partial or write a custom HTML helper. 在这种情况下,您可以将其外部化为部分或编写自定义HTML帮助程序。

I think it's better to implement your logic inside your controller instead of HTML codes, in these situation I try to do something like following : 我认为最好在控制器中实现逻辑而不是HTML代码,在这种情况下,我尝试执行以下操作:

    @if (ViewBag.SubCategoryHasData)
    {
        <img src="http://i.imgur.com/t5UXT.gif" />   
        <a href="#">@Model.Nombre</a>
        <p class="subtext">@Model.Encabezado</p>
    }
    else
    {
        <a class="nochild" href="#">@Model.Nombre</a>
        <p class="subtext nochild">@Model.Encabezado</p>
    }

and inside your action : 在你的行动中:

    ViewBag.SubCategoryHasData = Subcategorias.Count > 0;

hope this help. 希望这个帮助。

The way you do it is fine. 你这样做的方式很好。

Just two things to make sure your separation of concerns is ideal: 只需要做两件事就可以确保您的关注点分离是理想的:

  1. Use the IEnumerable.Any() as in 使用IEnumerable.Any(),

     Subcategorias.Any() 

    instad of 不干涉的

     Subcategorias.Count > 0; 

    This expresses your intent better and is in some cases faster (in some cases the Count needs to iterate over the whole list, while Any() needs to read only the first item). 这更好地表达了您的意图,并且在某些情况下更快(在某些情况下,Count需要遍历整个列表,而Any()需要只读取第一个项目)。

  2. Make sure your Model.Subcategorias is a plain List. 确保您的Model.Subcategorias是一个普通的List。 If you have received the data from an Entity Framework model, this might be a lazily evaluated proxy and may cause a database call. 如果您已从实体框架模型收到数据,则可能是一个延迟评估的代理,并可能导致数据库调用。

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

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