Пример #1
0
        public ActionResult AddNew(Guid? parentId)
        {
            ViewBag.AddNewTitle = AddNewTitle;

            var model = new CategoryModel
            {
                ParentId = parentId,
                CategoryType = CategoryType
            };

            if (parentId.HasValue)
            {
                var parent = SiteUtils.BuildCategoryListFromTree().FirstOrDefault(i => i.Id == parentId.Value);
                if (parent != null && parent.Childs.Any())
                {
                    model.SortOrder = parent.Childs.Max(i => i.SortOrder) + 1;
                }
            }
            else
            {
                var cates = CacheHelper.GetCategoryTreeList(CategoryType);
                if (cates.Any())
                {
                    model.SortOrder = cates.Max(i => i.SortOrder) + 1;
                }
            }

            return JsonObject(true, string.Empty, new
            {
                html = PartialViewToString("BaseView/Nhom/_edit", model)
            });
        }
Пример #2
0
        public ActionResult SaveFile(CategoryModel model)
        {
            if (Request.Files["menuFileImage"] != null && !string.IsNullOrWhiteSpace(Request.Files["menuFileImage"].FileName))
            {
                var file = Request.Files["menuFileImage"];

                var extension = Path.GetExtension(file.FileName).ToStr();

                if (!SiteUtils.IsImageFile(file.FileName))
                {
                    return JsonObject(false, BackendMessage.FileTypeIsInvalid);
                }

                if (!SiteUtils.ImageSizeIsValid(file.ContentLength))
                {
                    return JsonObject(false, BackendMessage.FileMaxSize5MB);
                }

                if (!string.IsNullOrWhiteSpace(model.OldImagePath))
                {
                    DeleteImageFile(model.OldImagePath);
                }

                model.ImagePath = Guid.NewGuid() + extension;

                var filePath = PhysicalDataFilePath(model.ImagePath);
                file.SaveAs(filePath);
            }
            else if (string.IsNullOrWhiteSpace(model.ImagePath) && !string.IsNullOrWhiteSpace(model.OldImagePath))
            {
                DeleteImageFile(model.OldImagePath);
            }

            #region save list image

            var listImagePath = new List<string>();

            for (var i = 0; i < Request.Files.AllKeys.Length; ++i)
            {
                var file = Request.Files[Request.Files.AllKeys[i]];
                if (file != null &&
                    !string.IsNullOrWhiteSpace(file.FileName) && Request.Files.AllKeys[i].StartsWith("fileImage"))
                {
                    var extension = Path.GetExtension(file.FileName).ToStr();

                    if (!SiteUtils.IsImageFile(file.FileName))
                    {
                        return JsonObject(false, BackendMessage.FileTypeIsInvalid);
                    }

                    if (!SiteUtils.ImageSizeIsValid(file.ContentLength))
                    {
                        return JsonObject(false, BackendMessage.FileMaxSize5MB);
                    }

                    var imagePath = Guid.NewGuid() + extension;
                    listImagePath.Add(imagePath);

                    var filePath = PhysicalDataFilePath(imagePath);
                    file.SaveAs(filePath);
                }
            }

            #endregion

            return JsonObject(true, string.Empty, new
            {
                imagePath = model.ImagePath,
                imageFileNames = string.Join(";", listImagePath)
            });
        }
Пример #3
0
        public ActionResult Save(CategoryModel model)
        {
            if (model.IsNew && !CanAdd)
            {
                return GetAddDeniedResult();
            }

            if (!model.IsNew && !CanUpdate)
            {
                return GetUpdateDeniedResult();
            }

            if (model.CategoryType != CategoryType.Product && model.CategoryType != CategoryType.UOM)
            {
                ModelState.Remove("Code");
            }

            if (!ModelState.IsValid)
            {
                return JsonObject(false, GetModelStateErrors());
            }

            var entity = model.Map<CategoryModel, Category>();

            if (entity.IsNew)
            {
                entity.InitId();
            }

            var request = new SaveRequest
            {
                Entity = entity
            };

            var res = ServiceHelper.Category.ExecuteDispose(s => s.SaveCategory(request));

            if (res.Success)
            {
                var indexModel = new CategoryIndexModel();
                PopulateIndexModel(indexModel);
                return JsonObject(true, BackendMessage.SaveDataSuccess, new
                {
                    html = PartialViewToString("BaseView/Nhom/_list", indexModel)
                });
            }

            var msg = res.Messages.FirstOrDefault();

            return JsonObject(false, string.IsNullOrWhiteSpace(msg) ? BackendMessage.ErrorOccure : msg.GetServiceMessageRes());
        }