Exemple #1
0
        public ActionResult CreateProductAttribute(AddProductAttributeVM attr)
        {
            if (ModelState.IsValid)
            {
                //add new ListId
                int listId = 0;
                if (attr.ComboboxValues?.Count > 0)
                {
                    listId = db.ComboboxValues.OrderByDescending(x => x.ListId).Count();
                    listId++;

                    foreach (var item in attr.ComboboxValues)
                    {
                        item.ListId            = listId;
                        item.FK_CategoryAttrId = attr.CategoryAttributeId;
                        item.FK_ProductAttrId  = null;
                    }
                }

                //TO DO: add attributes and bind it with caetegories etc
                var vm     = new AddProductAttributeVM();
                var attrVM = Mapper.Map <CategoryAttributeVM>(attr);
                var entity = Mapper.Map <CategoryAttributeEntity>(attrVM);
                db.CategoryAttributes.Add(entity);
                db.Entry(entity).State = EntityState.Added;

                db.SaveChanges();
            }
            else
            {
                return(View(attr));
            }

            return(RedirectToAction("ProductsAttributes", "Home", null));
        }
Exemple #2
0
        public ActionResult AddProductAttribute(AddProductAttributeVM attr)
        {
            //var cateoryAttrEntity = db.CategoryAttributes.Find(attr.PKAttributeId);
            //var cateoryAttrVM = Mapper.Map<CategoryAttributesVM>(cateoryAttrEntity);
            //cateoryAttrVM.Value =

            var productEntity = db.Products.Find(attr.CurrentProduct.ProductId);

            ProductAttributeVM pa = new ProductAttributeVM();


            //add new ListId
            int listId = 0;

            if (attr.ComboboxValues?.Count > 0)
            {
                listId = db.ComboboxValues.OrderByDescending(x => x.ListId).Count();
                listId++;

                foreach (var item in attr.ComboboxValues)
                {
                    item.ListId = listId;
                }
            }

            pa.FK_CategoryAttributes = attr.FK_CategoryAttributes;

            if (!string.IsNullOrEmpty(attr.Value))
            {
                pa.Value = attr.Value;
            }
            else if (attr.ComboboxValues?.Count > 0)
            {
                pa.ComboboxValues = attr.ComboboxValues;
            }

            var attrEntity = Mapper.Map <ProductAttributeEntity>(pa);

            productEntity.Attributes.Add(attrEntity);

            //db.ProductAttributes.Add(attrEntity);
            //product.Attributes.Add(pa);

            //var productEntity = Mapper.Map<ProductEntity>(product);
            //db.Products.Add(productEntity);
            db.Entry(productEntity).State = System.Data.Entity.EntityState.Modified;
            db.Entry(attrEntity).State    = System.Data.Entity.EntityState.Added;

            db.SaveChanges();

            return(RedirectToAction("Index", "Home"));
        }
Exemple #3
0
        public ActionResult AddProductAttribute(int?productId = null)
        {
            var vm = new AddProductAttributeVM();

            if (productId != null)
            {
                var product = Mapper.Map <ProductVM>(db.Products.Find(productId));
                vm.CurrentProduct       = product;
                vm.ProductOfAttributeId = productId;
            }

            vm.CategoryAttributeId = db.Products.Find(productId)?.CatId;

            vm.AllCategories = Mapper.Map <ICollection <CategoryVM> >(db.Categories.ToList());

            return(View(vm));
        }
Exemple #4
0
        public ActionResult EditProductAttribute(AddProductAttributeVM attr)
        {
            var attrVM = Mapper.Map <CategoryAttributeVM>(attr);

            var productAttribute = db.ProductAttributes.Find(attr.ProductAttributeId);

            productAttribute.Value          = attr.Value;
            productAttribute.ComboboxValues = Mapper.Map <ICollection <AttributeValueListEntity> >(attr.ComboboxValues);

            if (ModelState.IsValid)
            {
                db.ProductAttributes.Attach(productAttribute);
                db.Entry(productAttribute).State = EntityState.Modified;
                db.SaveChanges();
            }
            else
            {
                return(View(attr));
            }

            return(RedirectToAction("ProductsAttributes"));
        }