Exemple #1
0
        public ActionResult EditProduct(AdminEditProductViewModel model)
        {
            var cats = _categoryService.GetAllowedEditCategories(UsersRole, true);

            model.Categories = _categoryService.GetBaseSelectListCategories(cats);
            bool getval = false;

            if (ModelState.IsValid)
            {
                if (model.Content == null)
                {
                    model.Content = string.Empty;
                }

                using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                {
                    try
                    {
                        var product = _productSevice.Get(model.Id);
                        if (product == null)
                        {
                            return(RedirectToAction("index"));
                        }

                        ProductPost post;
                        if (product.ProductPost_Id != null)
                        {
                            post = _productPostSevice.Get((Guid)product.ProductPost_Id);
                            //model.Content = post.PostContent;
                        }
                        else
                        {
                            post                   = new ProductPost();
                            post.Product_Id        = product.Id;
                            product.ProductPost_Id = post.Id;
                        }

                        product.Name              = model.Name;
                        product.Category_Id       = model.Category;
                        product.Image             = model.Image;
                        product.IsLocked          = model.IsLocked;
                        product.ProductClassId    = model.ProductClass;
                        product.MembershipUser_Id = LoggedOnReadOnlyUser.Id;
                        post.PostContent          = model.Content;

                        product.ShotContent       = string.Concat(StringUtils.ReturnAmountWordsFromString(StringUtils.StripHtmlFromString(post.PostContent), 50), "....");
                        product.isAutoShotContent = true;
                        var i = product.ShotContent.Length;
                        if (i > 450)
                        {
                            product.ShotContent = product.ShotContent.Substring(0, 440) + "...";
                        }


                        if (model.AllAttribute != null)
                        {
                            foreach (var it in model.AllAttribute)
                            {
                                var a = _productSevice.GetAttribute(it.AttriId);
                                it.Name      = a.LangName;
                                it.ValueType = a.ValueType;
                                it.IsNull    = a.IsNull;

                                if (!a.ValueOption.IsNullEmpty() && a.ValueType == 2)
                                {
                                    dynamic json = JsonConvert.DeserializeObject(a.ValueOption);
                                    if (json != null)
                                    {
                                        it.ValueOptions = new List <string>();
                                        foreach (var itt in json)
                                        {
                                            it.ValueOptions.Add((string)itt);
                                        }
                                    }
                                }

                                //if (!a.ValueFindter.IsNullEmpty())
                                //{
                                //	dynamic json = JsonConvert.DeserializeObject(a.ValueFindter);
                                //	if (json != null)
                                //	{
                                //		it.FindterNums = new List<AdminAttributeNumberFindter>();
                                //		foreach (dynamic itt in json)
                                //		{
                                //			it.FindterNums.Add(new AdminAttributeNumberFindter { Name = (string)itt.Name });
                                //		}
                                //	}
                                //}

                                _productSevice.Set(product, a, it.Value);
                            }
                        }
                        getval = true;


                        _productPostSevice.Update(post);
                        _productSevice.Update(product);

                        unitOfWork.Commit();
                        // We use temp data because we are doing a redirect
                        TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                        {
                            Message     = "Cập nhật sản phẩm thành công!",
                            MessageType = GenericMessages.success
                        };

                        //return RedirectToAction("Product", new { id = model.ProductClass });
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LoggingService.Error(ex);
                        ModelState.AddModelError("", "Xảy ra lỗi khi cập nhật sản phẩm");

                        if (!getval)
                        {
                            foreach (var it in model.AllAttribute)
                            {
                                var a = _productSevice.GetAttribute(it.AttriId);
                                it.Name      = a.LangName;
                                it.ValueType = a.ValueType;
                                it.IsNull    = a.IsNull;
                            }
                        }
                    }
                }
            }


            return(View(model));
        }