Beispiel #1
0
        public ActionResult Create(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                var check_Code = ProductRepository.GetAllProductByType("gift").Any(x => x.Code == model.Code);
                if (check_Code)
                {
                    ViewBag.FailedMessage = "Mã " + model.Code + " bị trùng!";
                    return(View(model));
                }
                var Product = new Domain.Sale.Entities.Product();
                AutoMapper.Mapper.Map(model, Product);
                Product.IsDeleted      = false;
                Product.CreatedUserId  = WebSecurity.CurrentUserId;
                Product.ModifiedUserId = WebSecurity.CurrentUserId;
                Product.CreatedDate    = DateTime.Now;
                Product.ModifiedDate   = DateTime.Now;
                Product.Point          = 1;
                Product.NoInbound      = false;
                if (model.PriceInbound == null)
                {
                    Product.PriceInbound = 0;
                }

                Product.Code = Product.Code.Trim();

                if (Request.Files["file-image"] != null)
                {
                    var file = Request.Files["file-image"];
                    if (file.ContentLength > 0)
                    {
                        string image_name = "gift_" + Helpers.Common.ChuyenThanhKhongDau(Regex.Replace(Product.Code, @"\s+", "_")) + "." + file.FileName.Split('.').Last();
                        file.SaveAs(Server.MapPath("~/files/product/") + image_name);
                        Product.Image_Name = image_name;
                    }
                }

                ProductRepository.InsertProduct(Product);

                if (string.IsNullOrEmpty(Request["IsPopup"]) == false)
                {
                    return(RedirectToAction("Edit", new { Id = model.Id, IsPopup = Request["IsPopup"] }));
                }

                TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess;
                return(RedirectToAction("Index"));
            }

            string errors = string.Empty;

            foreach (var modalState in ModelState.Values)
            {
                errors += modalState.Value + ": ";
                foreach (var error in modalState.Errors)
                {
                    errors += error.ErrorMessage;
                }
            }

            ViewBag.errors = errors;

            return(View(model));
        }
Beispiel #2
0
        //[ValidateAntiForgeryToken]
        public ActionResult Create(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                var Product = new Domain.Sale.Entities.Product();
                AutoMapper.Mapper.Map(model, Product);
                Product.IsDeleted      = false;
                Product.CreatedUserId  = WebSecurity.CurrentUserId;
                Product.ModifiedUserId = WebSecurity.CurrentUserId;
                Product.CreatedDate    = DateTime.Now;
                Product.ModifiedDate   = DateTime.Now;
                if (model.PriceInbound == null)
                {
                    Product.PriceInbound = 0;
                }
                Product.Code = Product.Code.Trim();
                var path = System.Web.HttpContext.Current.Server.MapPath("~" + Helpers.Common.GetSetting("product-image-folder"));
                if (Request.Files["file-image"] != null)
                {
                    var file = Request.Files["file-image"];
                    if (file.ContentLength > 0)
                    {
                        string image_name = "product_" + Helpers.Common.ChuyenThanhKhongDau(Regex.Replace(Product.Code, @"\s+", "_")) + "." + file.FileName.Split('.').Last();
                        bool   isExists   = System.IO.Directory.Exists(path);
                        if (!isExists)
                        {
                            System.IO.Directory.CreateDirectory(path);
                        }
                        file.SaveAs(path + image_name);
                        Product.Image_Name = image_name;
                    }
                }

                productRepository.InsertProduct(Product);

                //tạo đặc tính động cho sản phẩm nếu có danh sách đặc tính động truyền vào và đặc tính đó phải có giá trị
                ObjectAttributeController.CreateOrUpdateForObject(Product.Id, model.AttributeValueList);

                if (string.IsNullOrEmpty(Request["IsPopup"]) == false)
                {
                    return(RedirectToAction("Edit", new { Id = model.Id, IsPopup = Request["IsPopup"] }));
                }

                TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess;
                return(RedirectToAction("Index"));
            }

            string errors = string.Empty;

            foreach (var modalState in ModelState.Values)
            {
                errors += modalState.Value + ": ";
                foreach (var error in modalState.Errors)
                {
                    errors += error.ErrorMessage;
                }
            }

            ViewBag.errors = errors;

            return(View(model));
        }
Beispiel #3
0
        //[ValidateAntiForgeryToken]
        public ActionResult Create(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                var Product = new Domain.Sale.Entities.Product();
                AutoMapper.Mapper.Map(model, Product);
                Product.IsDeleted      = false;
                Product.CreatedUserId  = WebSecurity.CurrentUserId;
                Product.ModifiedUserId = WebSecurity.CurrentUserId;
                Product.CreatedDate    = DateTime.Now;
                Product.ModifiedDate   = DateTime.Now;
                if (model.PriceInbound == null)
                {
                    Product.PriceInbound = 0;
                }


                if (model.IsServicePackage == true && model.ServicesInPackage != null && model.ServicesInPackage.Where(x => x.ProductId > 0).Count() > 0)
                {
                    XElement xmlElements = new XElement("Services",
                                                        model.ServicesInPackage.Select(s =>
                                                                                       new XElement("Service", new XAttribute("ProductId", s.ProductId), new XAttribute("Quantity", s.Quantity))
                                                                                       )
                                                        );
                    string xml = xmlElements.ToString();

                    Product.ServicesChild = xml;
                }


                if (Request.Files["file-image"] != null)
                {
                    var file = Request.Files["file-image"];
                    if (file.ContentLength > 0)
                    {
                        string image_name = "service_" + Helpers.Common.ChuyenThanhKhongDau(Regex.Replace(Product.Name, @"\s+", "_")) + "." + file.FileName.Split('.').Last();

                        file.SaveAs(Server.MapPath("~/files/product/") + image_name);
                        Product.Image_Name = image_name;
                    }
                }

                ProductRepository.InsertProduct(Product);

                //tạo đặc tính động cho sản phẩm nếu có danh sách đặc tính động truyền vào và đặc tính đó phải có giá trị
                ObjectAttributeController.CreateOrUpdateForObject(Product.Id, model.AttributeValueList);

                if (string.IsNullOrEmpty(Request["IsPopup"]) == false)
                {
                    return(RedirectToAction("Edit", new { Id = model.Id, IsPopup = Request["IsPopup"] }));
                }

                TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess;
                return(RedirectToAction("Index"));
            }

            string errors = string.Empty;

            foreach (var modalState in ModelState.Values)
            {
                errors += modalState.Value + ": ";
                foreach (var error in modalState.Errors)
                {
                    errors += error.ErrorMessage;
                }
            }

            ViewBag.errors = errors;

            ViewBag.service = ProductRepository.GetAllProduct().Where(x => x.IsServicePackage != true && x.Type == "service").AsEnumerable().Select(item => new SelectListItem {
                Text = item.Name, Value = item.Id.ToString()
            });
            return(View(model));
        }