Ejemplo n.º 1
0
        public ActionResult EditCategory(EditCategoryViewModel categoryViewModel)
        {
            if (ModelState.IsValid)
            {
                using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                {
                    try
                    {
                        var category = _categoryService.Get(categoryViewModel.Id);

                        category.Description = categoryViewModel.Description;
                        category.IsLocked = categoryViewModel.IsLocked;
                        category.Name = categoryViewModel.Name;
                        category.SortOrder = categoryViewModel.SortOrder;

                        if (categoryViewModel.ParentCategory != null)
                        {
                            category.ParentCategory = _categoryService.Get(categoryViewModel.ParentCategory.Value);
                        }
                        else
                        {
                            // Must access property (trigger lazy-loading) before we can set it to null (Entity Framework bug!!!)
                            var triggerEfLoad = category.ParentCategory;
                            category.ParentCategory = null;
                        }

                        _categoryService.UpdateSlugFromName(category);

                        TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                                                                        {
                                                                            Message = "Category Updated",
                                                                            MessageType = GenericMessages.success
                                                                        };

                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        LoggingService.Error(ex);
                        unitOfWork.Rollback();

                        TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                        {
                            Message = "Category Update Failed",
                            MessageType = GenericMessages.error
                        };
                    }
                }
            }

            return View("PopupConfirm");
        }
        public ActionResult EditCategory(EditCategoryViewModel categoryViewModel)
        {
            if (ModelState.IsValid)
            {
                using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                {
                    try
                    {
                        var category = _categoryService.Get(categoryViewModel.Id);

                        category.Description = categoryViewModel.Description;
                        category.IsLocked = categoryViewModel.IsLocked;
                        category.ModeratePosts = categoryViewModel.ModeratePosts;
                        category.ModerateTopics = categoryViewModel.ModerateTopics;
                        category.Name = categoryViewModel.Name;
                        category.SortOrder = categoryViewModel.SortOrder;
                        category.PageTitle = categoryViewModel.PageTitle;
                        category.MetaDescription = categoryViewModel.MetaDesc;

                        if (categoryViewModel.ParentCategory != null)
                        {
                            // Set the parent category
                            var parentCategory = _categoryService.Get(categoryViewModel.ParentCategory.Value);
                            category.ParentCategory = parentCategory;

                            // Append the path from the parent category
                            SortPath(category, parentCategory);
                        }
                        else
                        {
                            // Must access property (trigger lazy-loading) before we can set it to null (Entity Framework bug!!!)
                            var triggerEfLoad = category.ParentCategory;
                            category.ParentCategory = null;

                            // Also clear the path
                            category.Path = null;
                        }

                        _categoryService.UpdateSlugFromName(category);

                        TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                                                                        {
                                                                            Message = "Category Updated",
                                                                            MessageType = GenericMessages.success
                                                                        };

                        categoryViewModel = CreateEditCategoryViewModel(category);

                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        LoggingService.Error(ex);
                        unitOfWork.Rollback();

                        TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                        {
                            Message = "Category Update Failed",
                            MessageType = GenericMessages.error
                        };
                    }
                }
            }

            return View(categoryViewModel);
        }
Ejemplo n.º 3
0
        public ActionResult EditCategory(Guid id)
        {
            using (UnitOfWorkManager.NewUnitOfWork())
            {
                var category = _categoryService.Get(id);
                var categoryViewModel = new EditCategoryViewModel
                                            {
                                                Name = category.Name,
                                                Description = category.Description,
                                                IsLocked = category.IsLocked,
                                                SortOrder = category.SortOrder,
                                                Id = category.Id,
                                                DateCreated = category.DateCreated,
                                                NiceUrl = category.NiceUrl,
                                                ParentCategory = category.ParentCategory == null ? Guid.Empty : category.ParentCategory.Id,
                                                AllCategories = _categoryService.GetAll()
                                                    .Where(x => x.Id != category.Id)
                                                    .ToList(),
                                            };

                return View(categoryViewModel);
            }
        }
 private EditCategoryViewModel CreateEditCategoryViewModel(Category category)
 {
     var categoryViewModel = new EditCategoryViewModel
     {
         Name = category.Name,
         Description = category.Description,
         IsLocked = category.IsLocked,
         ModeratePosts = category.ModeratePosts == true,
         ModerateTopics = category.ModerateTopics == true,
         SortOrder = category.SortOrder,
         Id = category.Id,
         PageTitle = category.PageTitle,
         MetaDesc = category.MetaDescription,
         ParentCategory = category.ParentCategory == null ? Guid.Empty : category.ParentCategory.Id,
         AllCategories = _categoryService.GetAll()
             .Where(x => x.Id != category.Id)
             .ToList()
     };
     return categoryViewModel;
 }