-2

I have a Database table with following data,

Department | Section | Employee ID
-----------------------------------
Production | Padding | 102001
Production | Padding | 102002
Production | Padding | 102003
Production | Sewing  | 103001
Production | Sewing  | 103002
HR & admin | admin   | 107001

Now I want to get like following nested list in C# MVC5 razor view, Data can be passed with Model

<html><body>
<ul>
  <li>Production
     <ul>
        <li>Padding
           <ul>
              <li>102001</li>
              <li>102002</li>
              <li>102003</li>
           </ul>
        </li>
        <li>Sewing
           <ul>
              <li>103001</li>
              <li>103002</li>
           </ul>
        </li>
     </ul>
  </li>
  <li>HR & admin
     <ul>
        <li>Admin
           <ul>
              <li>107001</li>
           </ul>
        </li>
     </ul>
  </li>
</ul>
</body></html>

How can I implement this with C# MVC5? Kindly help me. I will use the list for jstree input.

Sumon Tito
  • 105
  • 10
  • You need to use `.GroupBy()` clauses to group you data first by `Department` an then by `Section` –  Sep 09 '18 at 10:04
  • Dear Stephen, only `.GroupBy()` may generate a distinct list but I want to generate code like second phase. – Sumon Tito Sep 09 '18 at 10:10
  • 1
    You group the data into a view model, and then you iterate it in the view using a loop –  Sep 09 '18 at 10:17
  • 2
    Suggest you look at the code in [this answer](https://stackoverflow.com/questions/43168802/rendering-a-sublist-of-a-list-in-net-mvc/43169088#43169088) to get you started –  Sep 09 '18 at 10:20

1 Answers1

0

Some people may not like this solution but it doesn't require any 3rd part libraries. The code uses just string methods.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Department", typeof(string));
            dt.Columns.Add("Section", typeof(string));
            dt.Columns.Add("Employee ID", typeof(string));

            dt.Rows.Add(new object[] {"Production","Padding","102001"});
            dt.Rows.Add(new object[] {"Production","Padding","102002"});
            dt.Rows.Add(new object[] {"Production","Padding","102003"});
            dt.Rows.Add(new object[] {"Production","Sewing","103001"});
            dt.Rows.Add(new object[] {"Production","Sewing","103002"});
            dt.Rows.Add(new object[] {"HR & admin","admin","107001"});

            CreateHtml createHtml = new CreateHtml(dt);

        }
    }
    public class CreateHtml
    {
        public DataTable dt = null;
        public  StringWriter writer = new StringWriter();
        private int numberColumns = 0;
        public CreateHtml(DataTable dt)
        {
            this.dt = dt;
            numberColumns = dt.Columns.Count;
            writer.WriteLine("<html><body>");
            RecursiveWrite(0, dt.AsEnumerable().ToList());
            writer.WriteLine("</html></body>");

            string body = writer.ToString();
        }
        void RecursiveWrite(int level, List<DataRow> rows)
        {
            string leader = new string(' ', 5 * level);
            writer.WriteLine("{0}  <ul>", leader);

            var groups = rows.GroupBy(x => x[level]).ToList();
            foreach (var group in groups)
            {
                if (level < numberColumns - 1)
                {
                    writer.WriteLine("{0}   <li>{1}", leader, group.Key);
                    RecursiveWrite(level + 1, group.ToList());
                    writer.WriteLine("{0}   </li>", leader);
                }
                else
                {
                    writer.WriteLine("{0}   <li>{1}</li>", leader, group.Key);
                }
            }

            writer.WriteLine("{0}  </ul>", leader);
        }
    }


}
jdweng
  • 33,250
  • 2
  • 15
  • 20