Пример #1
0
        public ActionResult EditProduct(Guid? id)
        {
            Product prod = null;

            if (id.HasValue)
            {
                using (var db = new qStoreDBEntities())
                {
                    prod = db.Products.SingleOrDefault(x => x.Id == id);
                }
            }

            if (prod == null)
            {
                prod = new Product
                {
                    Id = Guid.NewGuid()
                };
            }

            using (var db = new qStoreDBEntities())
            {
                var catList = db.Categories.Where(x => !x.IsDisabled).ToList()
                    .Select(x => new SelectListItem { Text = x.Title, Value = x.Id.ToString() }).ToList();

                ViewBag.Categories = catList;
            }

            return View(prod);
        }
Пример #2
0
        public ActionResult SaveProduct(HttpPostedFileBase file, Product model)
        {
            using (var db = new qStoreDBEntities())
            {
                var product = db.Products.SingleOrDefault(x => x.Id == model.Id);
                model.Category = db.Categories.SingleOrDefault(x => x.Id == model.CategoryId);

                if (file != null && file.ContentLength > 0)
                {
                    model.ImageContentType = file.ContentType;
                    model.ImageBase64String = GetByteArrayString(file);
                }

                if (product != null)
                {
                    product.Title = model.Title;
                    product.Description = model.Description;
                    product.Category = model.Category;
                    product.ImageBase64String = model.ImageBase64String;
                    product.ImageContentType = model.ImageContentType;
                    product.Price = model.Price;
                }
                else
                {
                    db.Products.Add(model);
                }

                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }