public ActionResult Add(AddCategoryViewModel viewModel)
 {
     if (!ModelState.IsValid)
         return View(viewModel);
     var category = new Category
     {
         Name = viewModel.Name,
         Parent = viewModel.Parent
     };
     using (var database = new DatabaseEntities())
     {
         database.AddToCategories(category);
         database.SaveChanges();
     }
     return RedirectToAction("Index");
 }
 /// <summary>
 /// Create a new Category object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="name">Initial value of the Name property.</param>
 public static Category CreateCategory(global::System.Int32 id, global::System.String name)
 {
     Category category = new Category();
     category.Id = id;
     category.Name = name;
     return category;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the Categories EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToCategories(Category category)
 {
     base.AddObject("Categories", category);
 }
        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;
        }