变化在树结构级联到儿童ASP.NET MVC 3 [英] Changes Cascading down to children in a Tree structure ASP.NET MVC 3

查看:68
本文介绍了变化在树结构级联到儿童ASP.NET MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个ASP.NET MVC 3应用程序,并在树结构升级的子节点的交易。
用户被允许进行更改到树中的一个节点的任何部分。一旦用户做出了改变,(即,一个状态字段),该变化将必须级联至所有的孩子。
问题是有小孩和他们的孩子任意数量的对儿童等任意号码。我怎么会去这样做呢?

This is for an ASP.NET MVC 3 application and deals with updating children nodes in a tree structure. The user is allowed to make changes to any part of a node in the tree. Once the user has made a change, (i.e. to a Status field) that change will have to be cascaded down to all the children. The issue is there are an arbitrary amount of children and their children have an arbitrary number on children and so on. How would I go about doing this?

感谢任何人谁可以帮助!

Thanks anyone who can help!

EDIT

我想为这个结构重演,直到有没有更多的留守儿童。

I would like for this structure to repeat itself until there are no more children left

if (item.child.Count > 0) //Level 1
{
   foreach (var item1 in item.child)
   {
      //Logic to update each entity 

      if (item1.child.Count > 0) //Level 2
      {
         foreach (var item2 in item1.child)
         {
            //Logic to update each entity 

            if (item2.child.Count > 0) //Level 3
            {
               foreach(var item3 in item2.child) 
                           .
                           .
                           .

是否有这样做的一个优雅的方式,或只是某种形式的硬编码此为最佳猜测号或水平?

Is there an elegant way of doing this, or is it just some form of hardcoding this in for a "best guess" number or levels?

推荐答案

您需要写一个递归方法:

You need to write a recursive method:

void ProcessChildren (Item item)
{
    // Logic to update 'this' child item

    foreach (var child in item.child)
    {
       ProcessChildren (child);
    }
}

ProcessChildren (rootItem);

这篇关于变化在树结构级联到儿童ASP.NET MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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