C#实现无限级联下拉列表框

本文实例为大家分享了无限级联下拉列表框的的实现方法,具体内容如下

可能有一个树型结构的表,它可能有ID,Name,ParentID,Level等字段,下面要实现的就是从一级节点开始,一级一级的列出来,并以

下拉列表框的形式体现出来,就像是N级联动。

效果图:

两个问题:

1、建立操作时的联动,它不需要进行自动绑定

2、编辑操作时的联运,它需要根据子节点,逐级自己绑定到父节点,直到根

实现:

JS代码

C#代码:

#region 树型结构相关
    /// 
    /// 递归找老祖宗
    /// 
    /// 
    void GetFather(SubItem father)
    {
      if (father != null)
      {
        father.Parent = _subList.FirstOrDefault(i => i.ID == father.ParentID);
        GetFather(father.Parent);
      }
    }

    /// 
    /// 弟妹找子孙
    /// 
    /// 父对象
    void getSons(SubItem father)
    {
      if (father != null)
      {
        father.Sons = _subList.Where(item =>
          item.ParentID.Equals(father.ID)).ToList();
        father.Sons.ForEach(item =>
        {
          item.Parent = father;
          getSons(item);
        });
      }
    }


    #endregion

C#拼接下拉列表框相关:

/// 
    /// 递归得到它的所有祖宗以selectlist的形式进行拼接
    /// 
    /// 
    /// 
    void getSelectList(SubItem son, StringBuilder sbr)
    {
      StringBuilder inSbr = new StringBuilder();
      if (son != null)
      {
        if (son.ParentID == 0)
          inSbr.Append("");
        GetCommon_CategoryByLevel(son.Level).ToList().ForEach(i =>
        {
          if (i.ID == son.ID)
            inSbr.Append("");
          else
            inSbr.Append("");
        });

        inSbr.Append("");
        sbr.Insert(0, inSbr);
        getSelectList(son.Parent, sbr);
      }
    }

C#得到同一深度的节点(同辈节点)相关:

/// 
    /// 得到指定深度的列表
    /// 
    /// 
    /// 
    public List GetCommon_CategoryByLevel(int level)
    {
      var linq = from data1 in _subList
            join data2 in _subList on data1.ParentID equals data2.ID into list
            select new SubItem
            {
              ID = data1.ID,
              Level = data1.Level,
              Name = data1.Name,
              Parent = list.FirstOrDefault(),
              ParentID = data1.ParentID,
            };
      return linq.Where(i => i.Level.Equals(level)).ToList();
    }

MVC页面action相关:

public ActionResult Category(int? id)
    {
      ViewData["Parent"] = new SelectList(_subList.Where(i => i.ID == (id ?? 0)), "ID", "Name", id ?? 1);
      SubItem current = _subList.FirstOrDefault(i => i.ID == (id ?? 1));
      GetFather(current);
      StringBuilder sbr = new StringBuilder();
      getSelectList(current, sbr);
      ViewData["edit"] = sbr.ToString();//修改时,进行绑定
      return View();
    }

MVC页面代码相关:

 @Html.Raw(ViewData["edit"].ToString())

C#树型结构实体类相关:

/// 
  /// 树型分类结构
  /// 
  public class Category
  {
    /// 
    /// 父ID
    /// 
    public int ParentID { get; set; }
    /// 
    /// 树ID
    /// 
    public int ID { get; set; }
    /// 
    /// 树名称
    /// 
    public string Name { get; set; }
    /// 
    /// 深度
    /// 
    public int Level { get; set; }
    /// 
    /// 子孙节点
    /// 
    public List Sons { get; set; }
    /// 
    /// 父节点
    /// 
    public Category Parent { get; set; }
  }

好了,现在我们的N级无限下拉列表框就做好了,感谢大家的阅读。

你可能感兴趣的:(C#实现无限级联下拉列表框)