linq - ASP MVC 5 C# Recursive Trees To Group and Indent A DropDownList -


i have model called category has recursive (parent child) relationship follows:

public class category: itreenode<category>     {         public byte id { get; set; }         public byte? parentid { get; set; }         public string name { get; set; }          public category parent { get; set; }         public ilist<category> children { get; set; }      } 

i want generate dropdownlist heirachaly grouped based on parent child relations can select parent group follows - want indent children parent:

 <select name="category" id="category">        <option value="0">all</option>        <option value="1">cars</option>        <option value="2">--toyota</option>        <option value="3">--nissan</option>        <option value="4">fruits</option>        <option value="5">--apples</option>        <option value="6">--oranges</option>     </select> 

my table data follows:

 id  | parentid  | name  ----------------------  1   |   null    |  cars  2   |   1       |  toyota  3   |   1       |  nissan  4   |   null    |  fruits  5   |   4       |  apples  6   |   4       |  oranges 

currently have following linq query generates normal dropdown ordered id.

    public ienumerable<selectlistitem> getcategoriesselectlist()     {         var categories = new list<selectlistitem>             {                 new selectlistitem() {value = "0", text = "all" }              }.concat(_context.category.select(x => new selectlistitem             {                 value = x.id.tostring(),                 text = x.name             }).orderby(x => x.value).tolist());          return categories     } 

how can amend linq query them correctly grouped , indented when it's rendered using html.dropdownlistfor.

i've tried amend original select list achieve kind of tree follows i'm stuck on enumeratenodes method. original below printing out ul list pulled following site http://www.codeproject.com/articles/23949/building-trees-from-lists-in-net. how iterate through , return each item, if child append -- name , add select list?

   public ienumerable<selectlistitem> getcategoriesselectlist()     {         ilist<category> listofnodes = getlistofnodes();         ilist<category> toplevelcategories = treehelper.converttoforest(listofnodes);         var cats = new list<selectlistitem>         {              new selectlistitem { value = "0", text = "all"}         };         foreach(var category in toplevelcategories) {             var catname = enumeratenodes(category);             cats.add(new selectlistitem { value = category.id.tostring(), text = catname });        }         return cats;     }      private list<category> getlistofnodes()    {      list<category> sourcecategories = _context.category.tolist();     list<category> categories = new list<category>();     foreach (category sourcecategory in sourcecategories)     {       category s = new category();       s.id = sourcecategory.id;       s.name = sourcecategory.name;       if (sourcecategory.parentid != null)       {           s.parent = new category();           s.parent.id = (int)sourcecategory.parentid;       }       categories.add(s);     }     return categories;   }   private static string enumeratenodes(category parent) {    if (category.children.count > 0) {       response.write("<ul>");       foreach(category child in category.children) {           enumeratenodes(child);       }       response.write("</ul>");    }    response.write("</li>"); } 

you can first select parent categories (where parentid null) , each parent category, select child categories.

public ienumerable<selectlistitem> getcategoriesselectlist() {     var categories = _context.category.tolist();     // initialise list , add first "all" item     list<selectlistitem> options = new list<selectlistitem>     {         new selectlistitem(){ value = "0", text = "all" }     };     // top level parents     var parents = categories.where(x => x.parentid == null);     foreach (var parent in parents)     {         // add selectlistitem parent         options.add(new selectlistitem()         {             value = parent.id.tostring(),             text = parent.name         });         // child items associated parent         var children = categories.where(x => x.parentid == parent.id);         // add selectlistitem each child         foreach (var child in children)         {             options.add(new selectlistitem()             {                  value = child.id.tostring(),                 text = string.format("--{0}",child.name)             });         }     }     return options; } 

Comments