示例#1
0
        private ViewResult getProduct(int id, string operation)
        {
            var product = new OfficeProductViewModel();

            this.load(product, operation, id);
            return(View("Product", product));
        }
示例#2
0
 public IActionResult Delete(OfficeProductViewModel vm)
 {
     this.Data.Products.Delete(vm.Product);
     this.Data.Save();
     TempData["message"] = $"{vm.Product.Name} removed from Products.";
     return(RedirectToAction("Search"));
 }
示例#3
0
        public IActionResult Edit(OfficeProductViewModel vm)
        {
            if (ModelState.IsValid)
            {
                this.Data.Products.Update(vm.Product);
                this.Data.Save();

                TempData["message"] = $"{vm.Product.Name} updated.";
                return(RedirectToAction("Search"));
            }

            this.load(vm, "Edit");
            return(View("Product", vm));
        }
示例#4
0
        private void load(OfficeProductViewModel vm, string op, int?id = null)
        {
            if (Operation.IsAdd(op))
            {
                vm.Product = new OfficeProduct();
            }
            else
            {
                vm.Product = this.Data.Products.Get(new QueryOptions <OfficeProduct> {
                    Include = "Type",
                    Where   = op => op.OfficeProductId == (id ?? vm.Product.OfficeProductId)
                });
            }

            vm.Types = this.Data.Types.List(new QueryOptions <ProductType> {
                OrderBy = pt => pt.Name
            });
        }
示例#5
0
        public IActionResult Add(OfficeProductViewModel vm)
        {
            if (ModelState.IsValid)
            {
                if (Request.Form.Files.Count == 0)
                {
                    this.load(vm, "Add");
                    ModelState.AddModelError("", "Please select an image.");
                    return(View("Product", vm));
                }

                foreach (var file in Request.Form.Files)
                {
                    var img = new Image();
                    img.Title = file.FileName;

                    var ms = new MemoryStream();
                    file.CopyTo(ms);
                    img.Data = ms.ToArray();

                    ms.Close();
                    ms.Dispose();

                    this.Data.Images.Insert(img);
                    this.Data.Save();

                    vm.Product.ImageId = img.ImageId;
                }

                this.Data.Products.Insert(vm.Product);
                this.Data.Save();

                TempData["message"] = $"{vm.Product.Name} added to Products.";
                return(RedirectToAction("Index"));
            }

            this.load(vm, "Add");
            return(View("Product", vm));
        }