zoukankan      html  css  js  c++  java
  • 在razor中使用递归,巧用递归

    原文发布时间为:2011-04-20 —— 来源于本人的百度文章 [由搬家工具导入]

    Learning Razor–Writing an Inline Recursive HTML Helper

    Writing an Inline Recursive Html Helper

    The Spark view engine provides you with the ability to define inline macros that allow for recursive calls (Read Louis DeJardin blog on the topic here, and additional documentation here).  These macros are extremely useful for rendering a hierarchical structure such as the one in the following snippet.

    <ul>
      <li>Fuller, Andrew
        <ul>
          <li>Davolio, Nancy</li>
          <li>Leverling, Janet</li>
          <li>Peacock, Margaret</li>
          <li>Buchanan, Steven
            <ul>
              <li>Suyama, Michael</li>
              <li>King, Robert</li>
              <li>Dodsworth, Anne</li>
            </ul>
          </li>
          <li>Callahan, Laura</li>
        </ul>
      </li>
    </ul>

    Now, the Razor view engine allows you to easily build a hierarchical structure by the use of inline HTML helpers that can be called recursively.  The following is a basic snippet of a “Hello World” inline HTML helper, and a call to trigger its functionality.

    @HelloWorld("Roberto!")

    @helper HelloWorld(string name) {
        <h1>Hello World! @name</h1>
    }

    Finally, if we wanted to output the html from the first snippet using Razor we could write the following inline HTML helper.

    @model IEnumerable<OrganizationNodeViewModel>
    @using MVC3.Playground.ViewModels;
    @{
        View.Title = "OrganizationTree";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h2>OrganizationTree</h2>

    @OrgTree(Model, null);

    @helper OrgTree(IEnumerable<OrganizationNodeViewModel> nodes, int? parentId) {
        if (nodes.Any(n => n.ParentId == parentId)) {
        <ul>
            @foreach (var node in nodes.Where(n => n.ParentId == parentId)) {
                <li>
                    @node.DisplayName
                    @OrgTree(nodes, node.Id)
                </li>
            }
        </ul>
        }
    }

  • 相关阅读:
    微信多业务
    jps命令
    三白话经典算法系列 Shell排序实现
    无法识别的属性“targetFramework”。请注意,属性名是大写和小写。错误的解决方案
    Timus 1777. Anindilyakwa 奇怪的问题计数
    tokumx经营报表
    Quartz CronTrigger应用
    HDU 3681 BFS&amp;像缩进DP&amp;二分法
    01背包问题
    Web模板
  • 原文地址:https://www.cnblogs.com/handboy/p/7163964.html
Copyright © 2011-2022 走看看