public ActionResult Index()
 {
     var viewModel = new NestedCategoryDescriptor();
     using (var database = new DatabaseEntities())
     {
         viewModel.Subcategories = new List<NestedCategoryDescriptor>();
         foreach (var category in database.Categories.Where(c => c.Parent == null))
             viewModel.Subcategories.Add(GenerateCategory(category));
     }
     return View(viewModel);
 }
        private NestedCategoryDescriptor GenerateCategory(Category category)
        {
            var descriptor = new NestedCategoryDescriptor();
            descriptor.Name = category.Name.Trim();
            descriptor.Id = category.Id;
            descriptor.Subcategories = new List<NestedCategoryDescriptor>();

            foreach (var subcategory in category.Children)
                descriptor.Subcategories.Add(GenerateCategory(subcategory));
            return descriptor;
        }