public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Category category = await _context.Categories.FindAsync(id);
            if (category == null)
            {
                return HttpNotFound();
            }

            // Wind-up a Category viewmodel
            CategoryViewModel categoryViewModel = new CategoryViewModel();
            categoryViewModel.Id = category.Id;
            categoryViewModel.ParentCategoryId = category.ParentCategoryId;
            categoryViewModel.Name = category.Name;

            ViewBag.ParentCategoryIdSelectList = PopulateParentCategorySelectList(categoryViewModel.Id);
            return View(categoryViewModel);
        }
        public async Task<ActionResult> Edit(CategoryViewModel form, HttpPostedFileBase file, HttpPostedFileBase linkFile)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    FileServices.UploadFile(file, Server, _fileDestination);
                    form.PicFileName = Path.GetFileName(file.FileName);
                }

                if (linkFile != null)
                {
                    FileServices.UploadFile(file, Server, _fileDestination);
                    form.LinkToFileName = Path.GetFileName(linkFile.FileName);
                }
                // Unwind back to a Category
                var editedCategory = new Category();
                {
                    editedCategory.Id = form.Id;
                    editedCategory.ParentCategoryId = form.ParentCategoryId;
                    editedCategory.Name = form.Name;
                    editedCategory.CategoryCode = form.CategoryCode;
                    editedCategory.PicFileName = form.PicFileName;
                    editedCategory.LinkToFileName = form.LinkToFileName;
                }

                _context.Entry(editedCategory).State = EntityState.Modified;
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.ParentCategoryIdSelectList = PopulateParentCategorySelectList(form.Id);
            return View(form);
        }
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Category category = await _context.Categories.FindAsync(id);
            if (category == null)
            {
                return HttpNotFound();
            }

            var categoryViewModel = new CategoryViewModel
            {
                Id = category.Id,
                ParentCategoryId = category.ParentCategoryId,
                Name = category.Name,
                CategoryCode = category.CategoryCode,
                PicFileName = category.PicFileName,
                LinkToFileName = category.LinkToFileName
            };

            ViewBag.ParentCategoryIdSelectList = PopulateParentCategorySelectList(categoryViewModel.Id);
            return View(categoryViewModel);
        }
        public async Task<ActionResult> Add(CategoryViewModel form, HttpPostedFileBase file, HttpPostedFileBase linkFile)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    FileServices.UploadFile(file, Server, _fileDestination);
                    form.PicFileName = Path.GetFileName(file.FileName);
                }
                if (linkFile != null)
                {
                    FileServices.UploadFile(file, Server, _fileDestination);
                    form.LinkToFileName = Path.GetFileName(linkFile.FileName);
                }
                var category = new Category
                {
                    ParentCategoryId = form.ParentCategoryId,
                    CategoryCode = form.CategoryCode,
                    Name = form.Name,
                    PicFileName = form.PicFileName,
                    LinkToFileName = form.LinkToFileName

                };

                _context.Categories.Add(category);
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ParentCategoryIdSelectList = PopulateParentCategorySelectList(null);
            return View(form);
        }
        public ActionResult Add(int id)
        {
            var category = _context.Categories.Find(id);

            var viewModel = new CategoryViewModel
            {
                ParentCategoryId = id,
                CategoryCode = category.CategoryCode,
                ParentName = category.Name
            };


            return View(viewModel);
        }
 public ActionResult Create()
 {
     var viewModel = new CategoryViewModel();
     ViewBag.ParentCategoryIdSelectList = PopulateParentCategorySelectList(null);
     return View(viewModel);
 }